ignore doxyout
[heimdal.git] / kdc / kerberos5.c
blobc7803ee0eecf6b20129b17190b875cdaf085bcde
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, &clientdb, &client);
992 if(ret){
993 const char *msg = krb5_get_error_message(context, ret);
994 kdc_log(context, config, 0, "UNKNOWN -- %s: %s", client_name, msg);
995 krb5_free_error_message(context, msg);
996 ret = KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN;
997 goto out;
1000 ret = _kdc_db_fetch(context, config, server_princ,
1001 HDB_F_GET_SERVER|HDB_F_GET_KRBTGT,
1002 NULL, &server);
1003 if(ret){
1004 const char *msg = krb5_get_error_message(context, ret);
1005 kdc_log(context, config, 0, "UNKNOWN -- %s: %s", server_name, msg);
1006 krb5_free_error_message(context, msg);
1007 ret = KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN;
1008 goto out;
1011 memset(&et, 0, sizeof(et));
1012 memset(&ek, 0, sizeof(ek));
1015 * Select a session enctype from the list of the crypto systems
1016 * supported enctype, is supported by the client and is one of the
1017 * enctype of the enctype of the krbtgt.
1019 * The later is used as a hint what enctype all KDC are supporting
1020 * to make sure a newer version of KDC wont generate a session
1021 * enctype that and older version of a KDC in the same realm can't
1022 * decrypt.
1024 * But if the KDC admin is paranoid and doesn't want to have "no
1025 * the best" enctypes on the krbtgt, lets save the best pick from
1026 * the client list and hope that that will work for any other
1027 * KDCs.
1030 const krb5_enctype *p;
1031 krb5_enctype clientbest = ETYPE_NULL;
1032 int i, j;
1034 p = krb5_kerberos_enctypes(context);
1036 sessionetype = ETYPE_NULL;
1038 for (i = 0; p[i] != ETYPE_NULL && sessionetype == ETYPE_NULL; i++) {
1039 if (krb5_enctype_valid(context, p[i]) != 0)
1040 continue;
1042 for (j = 0; j < b->etype.len && sessionetype == ETYPE_NULL; j++) {
1043 Key *dummy;
1044 /* check with client */
1045 if (p[i] != b->etype.val[j])
1046 continue;
1047 /* save best of union of { client, crypto system } */
1048 if (clientbest == ETYPE_NULL)
1049 clientbest = p[i];
1050 /* check with krbtgt */
1051 ret = hdb_enctype2key(context, &server->entry, p[i], &dummy);
1052 if (ret)
1053 continue;
1054 sessionetype = p[i];
1057 /* if krbtgt had no shared keys with client, pick clients best */
1058 if (clientbest != ETYPE_NULL && sessionetype == ETYPE_NULL) {
1059 sessionetype = clientbest;
1060 } else if (sessionetype == ETYPE_NULL) {
1061 kdc_log(context, config, 0,
1062 "Client (%s) from %s has no common enctypes with KDC"
1063 "to use for the session key",
1064 client_name, from);
1065 goto out;
1070 * Pre-auth processing
1073 if(req->padata){
1074 int i;
1075 const PA_DATA *pa;
1076 int found_pa = 0;
1078 log_patypes(context, config, req->padata);
1080 #ifdef PKINIT
1081 kdc_log(context, config, 5,
1082 "Looking for PKINIT pa-data -- %s", client_name);
1084 e_text = "No PKINIT PA found";
1086 i = 0;
1087 pa = _kdc_find_padata(req, &i, KRB5_PADATA_PK_AS_REQ);
1088 if (pa == NULL) {
1089 i = 0;
1090 pa = _kdc_find_padata(req, &i, KRB5_PADATA_PK_AS_REQ_WIN);
1092 if (pa) {
1093 char *client_cert = NULL;
1095 ret = _kdc_pk_rd_padata(context, config, req, pa, client, &pkp);
1096 if (ret) {
1097 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
1098 kdc_log(context, config, 5,
1099 "Failed to decode PKINIT PA-DATA -- %s",
1100 client_name);
1101 goto ts_enc;
1103 if (ret == 0 && pkp == NULL)
1104 goto ts_enc;
1106 ret = _kdc_pk_check_client(context,
1107 config,
1108 clientdb,
1109 client,
1110 pkp,
1111 &client_cert);
1112 if (ret) {
1113 e_text = "PKINIT certificate not allowed to "
1114 "impersonate principal";
1115 _kdc_pk_free_client_param(context, pkp);
1117 kdc_log(context, config, 0, "%s", e_text);
1118 pkp = NULL;
1119 goto out;
1122 found_pa = 1;
1123 et.flags.pre_authent = 1;
1124 kdc_log(context, config, 0,
1125 "PKINIT pre-authentication succeeded -- %s using %s",
1126 client_name, client_cert);
1127 free(client_cert);
1128 if (pkp)
1129 goto preauth_done;
1131 ts_enc:
1132 #endif
1133 kdc_log(context, config, 5, "Looking for ENC-TS pa-data -- %s",
1134 client_name);
1136 i = 0;
1137 e_text = "No ENC-TS found";
1138 while((pa = _kdc_find_padata(req, &i, KRB5_PADATA_ENC_TIMESTAMP))){
1139 krb5_data ts_data;
1140 PA_ENC_TS_ENC p;
1141 size_t len;
1142 EncryptedData enc_data;
1143 Key *pa_key;
1144 char *str;
1146 found_pa = 1;
1148 if (b->kdc_options.request_anonymous) {
1149 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
1150 kdc_log(context, config, 0, "ENC-TS doesn't support anon");
1151 goto out;
1154 ret = decode_EncryptedData(pa->padata_value.data,
1155 pa->padata_value.length,
1156 &enc_data,
1157 &len);
1158 if (ret) {
1159 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
1160 kdc_log(context, config, 5, "Failed to decode PA-DATA -- %s",
1161 client_name);
1162 goto out;
1165 ret = hdb_enctype2key(context, &client->entry,
1166 enc_data.etype, &pa_key);
1167 if(ret){
1168 char *estr;
1169 e_text = "No key matches pa-data";
1170 ret = KRB5KDC_ERR_ETYPE_NOSUPP;
1171 if(krb5_enctype_to_string(context, enc_data.etype, &estr))
1172 estr = NULL;
1173 if(estr == NULL)
1174 kdc_log(context, config, 5,
1175 "No client key matching pa-data (%d) -- %s",
1176 enc_data.etype, client_name);
1177 else
1178 kdc_log(context, config, 5,
1179 "No client key matching pa-data (%s) -- %s",
1180 estr, client_name);
1181 free(estr);
1182 free_EncryptedData(&enc_data);
1184 continue;
1187 try_next_key:
1188 ret = krb5_crypto_init(context, &pa_key->key, 0, &crypto);
1189 if (ret) {
1190 const char *msg = krb5_get_error_message(context, ret);
1191 kdc_log(context, config, 0, "krb5_crypto_init failed: %s", msg);
1192 krb5_free_error_message(context, msg);
1193 free_EncryptedData(&enc_data);
1194 continue;
1197 ret = krb5_decrypt_EncryptedData (context,
1198 crypto,
1199 KRB5_KU_PA_ENC_TIMESTAMP,
1200 &enc_data,
1201 &ts_data);
1202 krb5_crypto_destroy(context, crypto);
1204 * Since the user might have several keys with the same
1205 * enctype but with diffrent salting, we need to try all
1206 * the keys with the same enctype.
1208 if(ret){
1209 krb5_error_code ret2;
1210 const char *msg = krb5_get_error_message(context, ret);
1212 ret2 = krb5_enctype_to_string(context,
1213 pa_key->key.keytype, &str);
1214 if (ret2)
1215 str = NULL;
1216 kdc_log(context, config, 5,
1217 "Failed to decrypt PA-DATA -- %s "
1218 "(enctype %s) error %s",
1219 client_name, str ? str : "unknown enctype", msg);
1220 krb5_free_error_message(context, msg);
1221 free(str);
1223 if(hdb_next_enctype2key(context, &client->entry,
1224 enc_data.etype, &pa_key) == 0)
1225 goto try_next_key;
1226 e_text = "Failed to decrypt PA-DATA";
1228 free_EncryptedData(&enc_data);
1230 if (clientdb->hdb_auth_status)
1231 (clientdb->hdb_auth_status)(context, clientdb, client, HDB_AUTH_WRONG_PASSWORD);
1233 ret = KRB5KDC_ERR_PREAUTH_FAILED;
1234 continue;
1236 free_EncryptedData(&enc_data);
1237 ret = decode_PA_ENC_TS_ENC(ts_data.data,
1238 ts_data.length,
1240 &len);
1241 krb5_data_free(&ts_data);
1242 if(ret){
1243 e_text = "Failed to decode PA-ENC-TS-ENC";
1244 ret = KRB5KDC_ERR_PREAUTH_FAILED;
1245 kdc_log(context, config,
1246 5, "Failed to decode PA-ENC-TS_ENC -- %s",
1247 client_name);
1248 continue;
1250 free_PA_ENC_TS_ENC(&p);
1251 if (abs(kdc_time - p.patimestamp) > context->max_skew) {
1252 char client_time[100];
1254 krb5_format_time(context, p.patimestamp,
1255 client_time, sizeof(client_time), TRUE);
1257 ret = KRB5KRB_AP_ERR_SKEW;
1258 kdc_log(context, config, 0,
1259 "Too large time skew, "
1260 "client time %s is out by %u > %u seconds -- %s",
1261 client_time,
1262 (unsigned)abs(kdc_time - p.patimestamp),
1263 context->max_skew,
1264 client_name);
1267 * The following is needed to make windows clients to
1268 * retry using the timestamp in the error message, if
1269 * there is a e_text, they become unhappy.
1271 e_text = NULL;
1272 goto out;
1274 et.flags.pre_authent = 1;
1276 set_salt_padata(rep.padata, pa_key->salt);
1278 reply_key = &pa_key->key;
1280 ret = krb5_enctype_to_string(context, pa_key->key.keytype, &str);
1281 if (ret)
1282 str = NULL;
1284 kdc_log(context, config, 2,
1285 "ENC-TS Pre-authentication succeeded -- %s using %s",
1286 client_name, str ? str : "unknown enctype");
1287 free(str);
1288 break;
1290 #ifdef PKINIT
1291 preauth_done:
1292 #endif
1293 if(found_pa == 0 && config->require_preauth)
1294 goto use_pa;
1295 /* We come here if we found a pa-enc-timestamp, but if there
1296 was some problem with it, other than too large skew */
1297 if(found_pa && et.flags.pre_authent == 0){
1298 kdc_log(context, config, 0, "%s -- %s", e_text, client_name);
1299 e_text = NULL;
1300 goto out;
1302 }else if (config->require_preauth
1303 || b->kdc_options.request_anonymous /* hack to force anon */
1304 || client->entry.flags.require_preauth
1305 || server->entry.flags.require_preauth) {
1306 METHOD_DATA method_data;
1307 PA_DATA *pa;
1308 unsigned char *buf;
1309 size_t len;
1311 use_pa:
1312 method_data.len = 0;
1313 method_data.val = NULL;
1315 ret = realloc_method_data(&method_data);
1316 if (ret) {
1317 free_METHOD_DATA(&method_data);
1318 goto out;
1320 pa = &method_data.val[method_data.len-1];
1321 pa->padata_type = KRB5_PADATA_ENC_TIMESTAMP;
1322 pa->padata_value.length = 0;
1323 pa->padata_value.data = NULL;
1325 #ifdef PKINIT
1326 ret = realloc_method_data(&method_data);
1327 if (ret) {
1328 free_METHOD_DATA(&method_data);
1329 goto out;
1331 pa = &method_data.val[method_data.len-1];
1332 pa->padata_type = KRB5_PADATA_PK_AS_REQ;
1333 pa->padata_value.length = 0;
1334 pa->padata_value.data = NULL;
1336 ret = realloc_method_data(&method_data);
1337 if (ret) {
1338 free_METHOD_DATA(&method_data);
1339 goto out;
1341 pa = &method_data.val[method_data.len-1];
1342 pa->padata_type = KRB5_PADATA_PK_AS_REQ_WIN;
1343 pa->padata_value.length = 0;
1344 pa->padata_value.data = NULL;
1345 #endif
1348 * If there is a client key, send ETYPE_INFO{,2}
1350 ret = _kdc_find_etype(context, client, b->etype.val, b->etype.len,
1351 &ckey);
1352 if (ret == 0) {
1355 * RFC4120 requires:
1356 * - If the client only knows about old enctypes, then send
1357 * both info replies (we send 'info' first in the list).
1358 * - If the client is 'modern', because it knows about 'new'
1359 * enctype types, then only send the 'info2' reply.
1361 * Before we send the full list of etype-info data, we pick
1362 * the client key we would have used anyway below, just pick
1363 * that instead.
1366 if (older_enctype(ckey->key.keytype)) {
1367 ret = get_pa_etype_info(context, config,
1368 &method_data, ckey);
1369 if (ret) {
1370 free_METHOD_DATA(&method_data);
1371 goto out;
1374 ret = get_pa_etype_info2(context, config,
1375 &method_data, ckey);
1376 if (ret) {
1377 free_METHOD_DATA(&method_data);
1378 goto out;
1382 ASN1_MALLOC_ENCODE(METHOD_DATA, buf, len, &method_data, &len, ret);
1383 free_METHOD_DATA(&method_data);
1385 e_data.data = buf;
1386 e_data.length = len;
1387 e_text ="Need to use PA-ENC-TIMESTAMP/PA-PK-AS-REQ",
1389 ret = KRB5KDC_ERR_PREAUTH_REQUIRED;
1391 kdc_log(context, config, 0,
1392 "No preauth found, returning PREAUTH-REQUIRED -- %s",
1393 client_name);
1394 goto out;
1397 if (clientdb->hdb_auth_status)
1398 (clientdb->hdb_auth_status)(context, clientdb, client,
1399 HDB_AUTH_SUCCESS);
1402 * Verify flags after the user been required to prove its identity
1403 * with in a preauth mech.
1406 ret = _kdc_check_access(context, config, client, client_name,
1407 server, server_name,
1408 req, &e_data);
1409 if(ret)
1410 goto out;
1413 * Selelct the best encryption type for the KDC with out regard to
1414 * the client since the client never needs to read that data.
1417 ret = _kdc_get_preferred_key(context, config,
1418 server, server_name,
1419 &setype, &skey);
1420 if(ret)
1421 goto out;
1423 if(f.renew || f.validate || f.proxy || f.forwarded || f.enc_tkt_in_skey
1424 || (f.request_anonymous && !config->allow_anonymous)) {
1425 ret = KRB5KDC_ERR_BADOPTION;
1426 e_text = "Bad KDC options";
1427 kdc_log(context, config, 0, "Bad KDC options -- %s", client_name);
1428 goto out;
1431 rep.pvno = 5;
1432 rep.msg_type = krb_as_rep;
1434 ret = copy_Realm(&client->entry.principal->realm, &rep.crealm);
1435 if (ret)
1436 goto out;
1437 ret = _krb5_principal2principalname(&rep.cname, client->entry.principal);
1438 if (ret)
1439 goto out;
1441 rep.ticket.tkt_vno = 5;
1442 copy_Realm(&server->entry.principal->realm, &rep.ticket.realm);
1443 _krb5_principal2principalname(&rep.ticket.sname,
1444 server->entry.principal);
1445 /* java 1.6 expects the name to be the same type, lets allow that
1446 * uncomplicated name-types. */
1447 #define CNT(sp,t) (((sp)->sname->name_type) == KRB5_NT_##t)
1448 if (CNT(b, UNKNOWN) || CNT(b, PRINCIPAL) || CNT(b, SRV_INST) || CNT(b, SRV_HST) || CNT(b, SRV_XHST))
1449 rep.ticket.sname.name_type = b->sname->name_type;
1450 #undef CNT
1452 et.flags.initial = 1;
1453 if(client->entry.flags.forwardable && server->entry.flags.forwardable)
1454 et.flags.forwardable = f.forwardable;
1455 else if (f.forwardable) {
1456 e_text = "Ticket may not be forwardable";
1457 ret = KRB5KDC_ERR_POLICY;
1458 kdc_log(context, config, 0,
1459 "Ticket may not be forwardable -- %s", client_name);
1460 goto out;
1462 if(client->entry.flags.proxiable && server->entry.flags.proxiable)
1463 et.flags.proxiable = f.proxiable;
1464 else if (f.proxiable) {
1465 e_text = "Ticket may not be proxiable";
1466 ret = KRB5KDC_ERR_POLICY;
1467 kdc_log(context, config, 0,
1468 "Ticket may not be proxiable -- %s", client_name);
1469 goto out;
1471 if(client->entry.flags.postdate && server->entry.flags.postdate)
1472 et.flags.may_postdate = f.allow_postdate;
1473 else if (f.allow_postdate){
1474 e_text = "Ticket may not be postdate";
1475 ret = KRB5KDC_ERR_POLICY;
1476 kdc_log(context, config, 0,
1477 "Ticket may not be postdatable -- %s", client_name);
1478 goto out;
1481 /* check for valid set of addresses */
1482 if(!_kdc_check_addresses(context, config, b->addresses, from_addr)) {
1483 e_text = "Bad address list in requested";
1484 ret = KRB5KRB_AP_ERR_BADADDR;
1485 kdc_log(context, config, 0,
1486 "Bad address list requested -- %s", client_name);
1487 goto out;
1490 ret = copy_PrincipalName(&rep.cname, &et.cname);
1491 if (ret)
1492 goto out;
1493 ret = copy_Realm(&rep.crealm, &et.crealm);
1494 if (ret)
1495 goto out;
1498 time_t start;
1499 time_t t;
1501 start = et.authtime = kdc_time;
1503 if(f.postdated && req->req_body.from){
1504 ALLOC(et.starttime);
1505 start = *et.starttime = *req->req_body.from;
1506 et.flags.invalid = 1;
1507 et.flags.postdated = 1; /* XXX ??? */
1509 _kdc_fix_time(&b->till);
1510 t = *b->till;
1512 /* be careful not overflowing */
1514 if(client->entry.max_life)
1515 t = start + min(t - start, *client->entry.max_life);
1516 if(server->entry.max_life)
1517 t = start + min(t - start, *server->entry.max_life);
1518 #if 0
1519 t = min(t, start + realm->max_life);
1520 #endif
1521 et.endtime = t;
1522 if(f.renewable_ok && et.endtime < *b->till){
1523 f.renewable = 1;
1524 if(b->rtime == NULL){
1525 ALLOC(b->rtime);
1526 *b->rtime = 0;
1528 if(*b->rtime < *b->till)
1529 *b->rtime = *b->till;
1531 if(f.renewable && b->rtime){
1532 t = *b->rtime;
1533 if(t == 0)
1534 t = MAX_TIME;
1535 if(client->entry.max_renew)
1536 t = start + min(t - start, *client->entry.max_renew);
1537 if(server->entry.max_renew)
1538 t = start + min(t - start, *server->entry.max_renew);
1539 #if 0
1540 t = min(t, start + realm->max_renew);
1541 #endif
1542 ALLOC(et.renew_till);
1543 *et.renew_till = t;
1544 et.flags.renewable = 1;
1548 if (f.request_anonymous)
1549 et.flags.anonymous = 1;
1551 if(b->addresses){
1552 ALLOC(et.caddr);
1553 copy_HostAddresses(b->addresses, et.caddr);
1556 et.transited.tr_type = DOMAIN_X500_COMPRESS;
1557 krb5_data_zero(&et.transited.contents);
1559 /* The MIT ASN.1 library (obviously) doesn't tell lengths encoded
1560 * as 0 and as 0x80 (meaning indefinite length) apart, and is thus
1561 * incapable of correctly decoding SEQUENCE OF's of zero length.
1563 * To fix this, always send at least one no-op last_req
1565 * If there's a pw_end or valid_end we will use that,
1566 * otherwise just a dummy lr.
1568 ek.last_req.val = malloc(2 * sizeof(*ek.last_req.val));
1569 if (ek.last_req.val == NULL) {
1570 ret = ENOMEM;
1571 goto out;
1573 ek.last_req.len = 0;
1574 if (client->entry.pw_end
1575 && (config->kdc_warn_pwexpire == 0
1576 || kdc_time + config->kdc_warn_pwexpire >= *client->entry.pw_end)) {
1577 ek.last_req.val[ek.last_req.len].lr_type = LR_PW_EXPTIME;
1578 ek.last_req.val[ek.last_req.len].lr_value = *client->entry.pw_end;
1579 ++ek.last_req.len;
1581 if (client->entry.valid_end) {
1582 ek.last_req.val[ek.last_req.len].lr_type = LR_ACCT_EXPTIME;
1583 ek.last_req.val[ek.last_req.len].lr_value = *client->entry.valid_end;
1584 ++ek.last_req.len;
1586 if (ek.last_req.len == 0) {
1587 ek.last_req.val[ek.last_req.len].lr_type = LR_NONE;
1588 ek.last_req.val[ek.last_req.len].lr_value = 0;
1589 ++ek.last_req.len;
1591 ek.nonce = b->nonce;
1592 if (client->entry.valid_end || client->entry.pw_end) {
1593 ALLOC(ek.key_expiration);
1594 if (client->entry.valid_end) {
1595 if (client->entry.pw_end)
1596 *ek.key_expiration = min(*client->entry.valid_end,
1597 *client->entry.pw_end);
1598 else
1599 *ek.key_expiration = *client->entry.valid_end;
1600 } else
1601 *ek.key_expiration = *client->entry.pw_end;
1602 } else
1603 ek.key_expiration = NULL;
1604 ek.flags = et.flags;
1605 ek.authtime = et.authtime;
1606 if (et.starttime) {
1607 ALLOC(ek.starttime);
1608 *ek.starttime = *et.starttime;
1610 ek.endtime = et.endtime;
1611 if (et.renew_till) {
1612 ALLOC(ek.renew_till);
1613 *ek.renew_till = *et.renew_till;
1615 copy_Realm(&rep.ticket.realm, &ek.srealm);
1616 copy_PrincipalName(&rep.ticket.sname, &ek.sname);
1617 if(et.caddr){
1618 ALLOC(ek.caddr);
1619 copy_HostAddresses(et.caddr, ek.caddr);
1622 #if PKINIT
1623 if (pkp) {
1624 e_text = "Failed to build PK-INIT reply";
1625 ret = _kdc_pk_mk_pa_reply(context, config, pkp, client,
1626 sessionetype, req, req_buffer,
1627 &reply_key, &et.key, rep.padata);
1628 if (ret)
1629 goto out;
1630 ret = _kdc_add_inital_verified_cas(context,
1631 config,
1632 pkp,
1633 &et);
1634 if (ret)
1635 goto out;
1637 } else
1638 #endif
1640 ret = krb5_generate_random_keyblock(context, sessionetype, &et.key);
1641 if (ret)
1642 goto out;
1645 if (reply_key == NULL) {
1646 e_text = "Client have no reply key";
1647 ret = KRB5KDC_ERR_CLIENT_NOTYET;
1648 goto out;
1651 ret = copy_EncryptionKey(&et.key, &ek.key);
1652 if (ret)
1653 goto out;
1655 /* Add signing of alias referral */
1656 if (f.canonicalize) {
1657 PA_ClientCanonicalized canon;
1658 krb5_data data;
1659 PA_DATA pa;
1660 krb5_crypto crypto;
1661 size_t len;
1663 memset(&canon, 0, sizeof(canon));
1665 canon.names.requested_name = *b->cname;
1666 canon.names.mapped_name = client->entry.principal->name;
1668 ASN1_MALLOC_ENCODE(PA_ClientCanonicalizedNames, data.data, data.length,
1669 &canon.names, &len, ret);
1670 if (ret)
1671 goto out;
1672 if (data.length != len)
1673 krb5_abortx(context, "internal asn.1 error");
1675 /* sign using "returned session key" */
1676 ret = krb5_crypto_init(context, &et.key, 0, &crypto);
1677 if (ret) {
1678 free(data.data);
1679 goto out;
1682 ret = krb5_create_checksum(context, crypto,
1683 KRB5_KU_CANONICALIZED_NAMES, 0,
1684 data.data, data.length,
1685 &canon.canon_checksum);
1686 free(data.data);
1687 krb5_crypto_destroy(context, crypto);
1688 if (ret)
1689 goto out;
1691 ASN1_MALLOC_ENCODE(PA_ClientCanonicalized, data.data, data.length,
1692 &canon, &len, ret);
1693 free_Checksum(&canon.canon_checksum);
1694 if (ret)
1695 goto out;
1696 if (data.length != len)
1697 krb5_abortx(context, "internal asn.1 error");
1699 pa.padata_type = KRB5_PADATA_CLIENT_CANONICALIZED;
1700 pa.padata_value = data;
1701 ret = add_METHOD_DATA(rep.padata, &pa);
1702 free(data.data);
1703 if (ret)
1704 goto out;
1707 if (rep.padata->len == 0) {
1708 free(rep.padata);
1709 rep.padata = NULL;
1712 /* Add the PAC */
1713 if (send_pac_p(context, req)) {
1714 krb5_pac p = NULL;
1715 krb5_data data;
1717 ret = _kdc_pac_generate(context, client, &p);
1718 if (ret) {
1719 kdc_log(context, config, 0, "PAC generation failed for -- %s",
1720 client_name);
1721 goto out;
1723 if (p != NULL) {
1724 ret = _krb5_pac_sign(context, p, et.authtime,
1725 client->entry.principal,
1726 &skey->key, /* Server key */
1727 &skey->key, /* FIXME: should be krbtgt key */
1728 &data);
1729 krb5_pac_free(context, p);
1730 if (ret) {
1731 kdc_log(context, config, 0, "PAC signing failed for -- %s",
1732 client_name);
1733 goto out;
1736 ret = _kdc_tkt_add_if_relevant_ad(context, &et,
1737 KRB5_AUTHDATA_WIN2K_PAC,
1738 &data);
1739 krb5_data_free(&data);
1740 if (ret)
1741 goto out;
1745 _kdc_log_timestamp(context, config, "AS-REQ", et.authtime, et.starttime,
1746 et.endtime, et.renew_till);
1748 /* do this as the last thing since this signs the EncTicketPart */
1749 ret = _kdc_add_KRB5SignedPath(context,
1750 config,
1751 server,
1752 setype,
1753 client->entry.principal,
1754 NULL,
1755 NULL,
1756 &et);
1757 if (ret)
1758 goto out;
1760 log_as_req(context, config, reply_key->keytype, setype, b);
1762 ret = _kdc_encode_reply(context, config,
1763 &rep, &et, &ek, setype, server->entry.kvno,
1764 &skey->key, client->entry.kvno,
1765 reply_key, 0, &e_text, reply);
1766 free_EncTicketPart(&et);
1767 free_EncKDCRepPart(&ek);
1768 if (ret)
1769 goto out;
1771 /* */
1772 if (datagram_reply && reply->length > config->max_datagram_reply_length) {
1773 krb5_data_free(reply);
1774 ret = KRB5KRB_ERR_RESPONSE_TOO_BIG;
1775 e_text = "Reply packet too large";
1778 out:
1779 free_AS_REP(&rep);
1780 if(ret){
1781 krb5_mk_error(context,
1782 ret,
1783 e_text,
1784 (e_data.data ? &e_data : NULL),
1785 client_princ,
1786 server_princ,
1787 NULL,
1788 NULL,
1789 reply);
1790 ret = 0;
1792 #ifdef PKINIT
1793 if (pkp)
1794 _kdc_pk_free_client_param(context, pkp);
1795 #endif
1796 if (e_data.data)
1797 free(e_data.data);
1798 if (client_princ)
1799 krb5_free_principal(context, client_princ);
1800 free(client_name);
1801 if (server_princ)
1802 krb5_free_principal(context, server_princ);
1803 free(server_name);
1804 if(client)
1805 _kdc_free_ent(context, client);
1806 if(server)
1807 _kdc_free_ent(context, server);
1808 return ret;
1812 * Add the AuthorizationData `data´ of `type´ to the last element in
1813 * the sequence of authorization_data in `tkt´ wrapped in an IF_RELEVANT
1816 krb5_error_code
1817 _kdc_tkt_add_if_relevant_ad(krb5_context context,
1818 EncTicketPart *tkt,
1819 int type,
1820 const krb5_data *data)
1822 krb5_error_code ret;
1823 size_t size;
1825 if (tkt->authorization_data == NULL) {
1826 tkt->authorization_data = calloc(1, sizeof(*tkt->authorization_data));
1827 if (tkt->authorization_data == NULL) {
1828 krb5_set_error_message(context, ENOMEM, "out of memory");
1829 return ENOMEM;
1833 /* add the entry to the last element */
1835 AuthorizationData ad = { 0, NULL };
1836 AuthorizationDataElement ade;
1838 ade.ad_type = type;
1839 ade.ad_data = *data;
1841 ret = add_AuthorizationData(&ad, &ade);
1842 if (ret) {
1843 krb5_set_error_message(context, ret, "add AuthorizationData failed");
1844 return ret;
1847 ade.ad_type = KRB5_AUTHDATA_IF_RELEVANT;
1849 ASN1_MALLOC_ENCODE(AuthorizationData,
1850 ade.ad_data.data, ade.ad_data.length,
1851 &ad, &size, ret);
1852 free_AuthorizationData(&ad);
1853 if (ret) {
1854 krb5_set_error_message(context, ret, "ASN.1 encode of "
1855 "AuthorizationData failed");
1856 return ret;
1858 if (ade.ad_data.length != size)
1859 krb5_abortx(context, "internal asn.1 encoder error");
1861 ret = add_AuthorizationData(tkt->authorization_data, &ade);
1862 der_free_octet_string(&ade.ad_data);
1863 if (ret) {
1864 krb5_set_error_message(context, ret, "add AuthorizationData failed");
1865 return ret;
1869 return 0;