Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / crypto / dist / heimdal / kdc / kerberos5.c
blob9cb5833fa438f32d0aee14bccb8fcfba586ed5c7
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 __RCSID("$Heimdal: kerberos5.c 22071 2007-11-14 20:04:50Z lha $"
37 "$NetBSD$");
39 #define MAX_TIME ((time_t)((1U << 31) - 1))
41 void
42 _kdc_fix_time(time_t **t)
44 if(*t == NULL){
45 ALLOC(*t);
46 **t = MAX_TIME;
48 if(**t == 0) **t = MAX_TIME; /* fix for old clients */
51 static int
52 realloc_method_data(METHOD_DATA *md)
54 PA_DATA *pa;
55 pa = realloc(md->val, (md->len + 1) * sizeof(*md->val));
56 if(pa == NULL)
57 return ENOMEM;
58 md->val = pa;
59 md->len++;
60 return 0;
63 static void
64 set_salt_padata (METHOD_DATA *md, Salt *salt)
66 if (salt) {
67 realloc_method_data(md);
68 md->val[md->len - 1].padata_type = salt->type;
69 der_copy_octet_string(&salt->salt,
70 &md->val[md->len - 1].padata_value);
74 const PA_DATA*
75 _kdc_find_padata(const KDC_REQ *req, int *start, int type)
77 if (req->padata == NULL)
78 return NULL;
80 while(*start < req->padata->len){
81 (*start)++;
82 if(req->padata->val[*start - 1].padata_type == type)
83 return &req->padata->val[*start - 1];
85 return NULL;
89 * Detect if `key' is the using the the precomputed `default_salt'.
92 static krb5_boolean
93 is_default_salt_p(const krb5_salt *default_salt, const Key *key)
95 if (key->salt == NULL)
96 return TRUE;
97 if (default_salt->salttype != key->salt->type)
98 return FALSE;
99 if (krb5_data_cmp(&default_salt->saltvalue, &key->salt->salt))
100 return FALSE;
101 return TRUE;
105 * return the first appropriate key of `princ' in `ret_key'. Look for
106 * all the etypes in (`etypes', `len'), stopping as soon as we find
107 * one, but preferring one that has default salt
110 krb5_error_code
111 _kdc_find_etype(krb5_context context, const hdb_entry_ex *princ,
112 krb5_enctype *etypes, unsigned len,
113 Key **ret_key, krb5_enctype *ret_etype)
115 int i;
116 krb5_error_code ret = KRB5KDC_ERR_ETYPE_NOSUPP;
117 krb5_salt def_salt;
119 krb5_get_pw_salt (context, princ->entry.principal, &def_salt);
121 for(i = 0; ret != 0 && i < len ; i++) {
122 Key *key = NULL;
124 if (krb5_enctype_valid(context, etypes[i]) != 0)
125 continue;
127 while (hdb_next_enctype2key(context, &princ->entry, etypes[i], &key) == 0) {
128 if (key->key.keyvalue.length == 0) {
129 ret = KRB5KDC_ERR_NULL_KEY;
130 continue;
132 *ret_key = key;
133 *ret_etype = etypes[i];
134 ret = 0;
135 if (is_default_salt_p(&def_salt, key)) {
136 krb5_free_salt (context, def_salt);
137 return ret;
141 krb5_free_salt (context, def_salt);
142 return ret;
145 krb5_error_code
146 _kdc_make_anonymous_principalname (PrincipalName *pn)
148 pn->name_type = KRB5_NT_PRINCIPAL;
149 pn->name_string.len = 1;
150 pn->name_string.val = malloc(sizeof(*pn->name_string.val));
151 if (pn->name_string.val == NULL)
152 return ENOMEM;
153 pn->name_string.val[0] = strdup("anonymous");
154 if (pn->name_string.val[0] == NULL) {
155 free(pn->name_string.val);
156 pn->name_string.val = NULL;
157 return ENOMEM;
159 return 0;
162 void
163 _kdc_log_timestamp(krb5_context context,
164 krb5_kdc_configuration *config,
165 const char *type,
166 KerberosTime authtime, KerberosTime *starttime,
167 KerberosTime endtime, KerberosTime *renew_till)
169 char authtime_str[100], starttime_str[100],
170 endtime_str[100], renewtime_str[100];
172 krb5_format_time(context, authtime,
173 authtime_str, sizeof(authtime_str), TRUE);
174 if (starttime)
175 krb5_format_time(context, *starttime,
176 starttime_str, sizeof(starttime_str), TRUE);
177 else
178 strlcpy(starttime_str, "unset", sizeof(starttime_str));
179 krb5_format_time(context, endtime,
180 endtime_str, sizeof(endtime_str), TRUE);
181 if (renew_till)
182 krb5_format_time(context, *renew_till,
183 renewtime_str, sizeof(renewtime_str), TRUE);
184 else
185 strlcpy(renewtime_str, "unset", sizeof(renewtime_str));
187 kdc_log(context, config, 5,
188 "%s authtime: %s starttime: %s endtime: %s renew till: %s",
189 type, authtime_str, starttime_str, endtime_str, renewtime_str);
192 static void
193 log_patypes(krb5_context context,
194 krb5_kdc_configuration *config,
195 METHOD_DATA *padata)
197 struct rk_strpool *p = NULL;
198 char *str;
199 int i;
201 for (i = 0; i < padata->len; i++) {
202 switch(padata->val[i].padata_type) {
203 case KRB5_PADATA_PK_AS_REQ:
204 p = rk_strpoolprintf(p, "PK-INIT(ietf)");
205 break;
206 case KRB5_PADATA_PK_AS_REQ_WIN:
207 p = rk_strpoolprintf(p, "PK-INIT(win2k)");
208 break;
209 case KRB5_PADATA_PA_PK_OCSP_RESPONSE:
210 p = rk_strpoolprintf(p, "OCSP");
211 break;
212 case KRB5_PADATA_ENC_TIMESTAMP:
213 p = rk_strpoolprintf(p, "encrypted-timestamp");
214 break;
215 default:
216 p = rk_strpoolprintf(p, "%d", padata->val[i].padata_type);
217 break;
219 if (p && i + 1 < padata->len)
220 p = rk_strpoolprintf(p, ", ");
221 if (p == NULL) {
222 kdc_log(context, config, 0, "out of memory");
223 return;
226 if (p == NULL)
227 p = rk_strpoolprintf(p, "none");
229 str = rk_strpoolcollect(p);
230 kdc_log(context, config, 0, "Client sent patypes: %s", str);
231 free(str);
239 krb5_error_code
240 _kdc_encode_reply(krb5_context context,
241 krb5_kdc_configuration *config,
242 KDC_REP *rep, const EncTicketPart *et, EncKDCRepPart *ek,
243 krb5_enctype etype,
244 int skvno, const EncryptionKey *skey,
245 int ckvno, const EncryptionKey *ckey,
246 const char **e_text,
247 krb5_data *reply)
249 unsigned char *buf;
250 size_t buf_size;
251 size_t len;
252 krb5_error_code ret;
253 krb5_crypto crypto;
255 ASN1_MALLOC_ENCODE(EncTicketPart, buf, buf_size, et, &len, ret);
256 if(ret) {
257 kdc_log(context, config, 0, "Failed to encode ticket: %s",
258 krb5_get_err_text(context, ret));
259 return ret;
261 if(buf_size != len) {
262 free(buf);
263 kdc_log(context, config, 0, "Internal error in ASN.1 encoder");
264 *e_text = "KDC internal error";
265 return KRB5KRB_ERR_GENERIC;
268 ret = krb5_crypto_init(context, skey, etype, &crypto);
269 if (ret) {
270 free(buf);
271 kdc_log(context, config, 0, "krb5_crypto_init failed: %s",
272 krb5_get_err_text(context, ret));
273 return ret;
276 ret = krb5_encrypt_EncryptedData(context,
277 crypto,
278 KRB5_KU_TICKET,
279 buf,
280 len,
281 skvno,
282 &rep->ticket.enc_part);
283 free(buf);
284 krb5_crypto_destroy(context, crypto);
285 if(ret) {
286 kdc_log(context, config, 0, "Failed to encrypt data: %s",
287 krb5_get_err_text(context, ret));
288 return ret;
291 if(rep->msg_type == krb_as_rep && !config->encode_as_rep_as_tgs_rep)
292 ASN1_MALLOC_ENCODE(EncASRepPart, buf, buf_size, ek, &len, ret);
293 else
294 ASN1_MALLOC_ENCODE(EncTGSRepPart, buf, buf_size, ek, &len, ret);
295 if(ret) {
296 kdc_log(context, config, 0, "Failed to encode KDC-REP: %s",
297 krb5_get_err_text(context, ret));
298 return ret;
300 if(buf_size != len) {
301 free(buf);
302 kdc_log(context, config, 0, "Internal error in ASN.1 encoder");
303 *e_text = "KDC internal error";
304 return KRB5KRB_ERR_GENERIC;
306 ret = krb5_crypto_init(context, ckey, 0, &crypto);
307 if (ret) {
308 free(buf);
309 kdc_log(context, config, 0, "krb5_crypto_init failed: %s",
310 krb5_get_err_text(context, ret));
311 return ret;
313 if(rep->msg_type == krb_as_rep) {
314 krb5_encrypt_EncryptedData(context,
315 crypto,
316 KRB5_KU_AS_REP_ENC_PART,
317 buf,
318 len,
319 ckvno,
320 &rep->enc_part);
321 free(buf);
322 ASN1_MALLOC_ENCODE(AS_REP, buf, buf_size, rep, &len, ret);
323 } else {
324 krb5_encrypt_EncryptedData(context,
325 crypto,
326 KRB5_KU_TGS_REP_ENC_PART_SESSION,
327 buf,
328 len,
329 ckvno,
330 &rep->enc_part);
331 free(buf);
332 ASN1_MALLOC_ENCODE(TGS_REP, buf, buf_size, rep, &len, ret);
334 krb5_crypto_destroy(context, crypto);
335 if(ret) {
336 kdc_log(context, config, 0, "Failed to encode KDC-REP: %s",
337 krb5_get_err_text(context, ret));
338 return ret;
340 if(buf_size != len) {
341 free(buf);
342 kdc_log(context, config, 0, "Internal error in ASN.1 encoder");
343 *e_text = "KDC internal error";
344 return KRB5KRB_ERR_GENERIC;
346 reply->data = buf;
347 reply->length = buf_size;
348 return 0;
352 * Return 1 if the client have only older enctypes, this is for
353 * determining if the server should send ETYPE_INFO2 or not.
356 static int
357 older_enctype(krb5_enctype enctype)
359 switch (enctype) {
360 case ETYPE_DES_CBC_CRC:
361 case ETYPE_DES_CBC_MD4:
362 case ETYPE_DES_CBC_MD5:
363 case ETYPE_DES3_CBC_SHA1:
364 case ETYPE_ARCFOUR_HMAC_MD5:
365 case ETYPE_ARCFOUR_HMAC_MD5_56:
367 * The following three is "old" windows enctypes and is needed for
368 * windows 2000 hosts.
370 case ETYPE_ARCFOUR_MD4:
371 case ETYPE_ARCFOUR_HMAC_OLD:
372 case ETYPE_ARCFOUR_HMAC_OLD_EXP:
373 return 1;
374 default:
375 return 0;
379 static int
380 only_older_enctype_p(const KDC_REQ *req)
382 int i;
384 for(i = 0; i < req->req_body.etype.len; i++) {
385 if (!older_enctype(req->req_body.etype.val[i]))
386 return 0;
388 return 1;
395 static krb5_error_code
396 make_etype_info_entry(krb5_context context, ETYPE_INFO_ENTRY *ent, Key *key)
398 ent->etype = key->key.keytype;
399 if(key->salt){
400 #if 0
401 ALLOC(ent->salttype);
403 if(key->salt->type == hdb_pw_salt)
404 *ent->salttype = 0; /* or 1? or NULL? */
405 else if(key->salt->type == hdb_afs3_salt)
406 *ent->salttype = 2;
407 else {
408 kdc_log(context, config, 0, "unknown salt-type: %d",
409 key->salt->type);
410 return KRB5KRB_ERR_GENERIC;
412 /* according to `the specs', we can't send a salt if
413 we have AFS3 salted key, but that requires that you
414 *know* what cell you are using (e.g by assuming
415 that the cell is the same as the realm in lower
416 case) */
417 #elif 0
418 ALLOC(ent->salttype);
419 *ent->salttype = key->salt->type;
420 #else
422 * We shouldn't sent salttype since it is incompatible with the
423 * specification and it breaks windows clients. The afs
424 * salting problem is solved by using KRB5-PADATA-AFS3-SALT
425 * implemented in Heimdal 0.7 and later.
427 ent->salttype = NULL;
428 #endif
429 krb5_copy_data(context, &key->salt->salt,
430 &ent->salt);
431 } else {
432 /* we return no salt type at all, as that should indicate
433 * the default salt type and make everybody happy. some
434 * systems (like w2k) dislike being told the salt type
435 * here. */
437 ent->salttype = NULL;
438 ent->salt = NULL;
440 return 0;
443 static krb5_error_code
444 get_pa_etype_info(krb5_context context,
445 krb5_kdc_configuration *config,
446 METHOD_DATA *md, hdb_entry *client,
447 ENCTYPE *etypes, unsigned int etypes_len)
449 krb5_error_code ret = 0;
450 int i, j;
451 unsigned int n = 0;
452 ETYPE_INFO pa;
453 unsigned char *buf;
454 size_t len;
457 pa.len = client->keys.len;
458 if(pa.len > UINT_MAX/sizeof(*pa.val))
459 return ERANGE;
460 pa.val = malloc(pa.len * sizeof(*pa.val));
461 if(pa.val == NULL)
462 return ENOMEM;
463 memset(pa.val, 0, pa.len * sizeof(*pa.val));
465 for(i = 0; i < client->keys.len; i++) {
466 for (j = 0; j < n; j++)
467 if (pa.val[j].etype == client->keys.val[i].key.keytype)
468 goto skip1;
469 for(j = 0; j < etypes_len; j++) {
470 if(client->keys.val[i].key.keytype == etypes[j]) {
471 if (krb5_enctype_valid(context, etypes[j]) != 0)
472 continue;
473 if (!older_enctype(etypes[j]))
474 continue;
475 if (n >= pa.len)
476 krb5_abortx(context, "internal error: n >= p.len");
477 if((ret = make_etype_info_entry(context,
478 &pa.val[n++],
479 &client->keys.val[i])) != 0) {
480 free_ETYPE_INFO(&pa);
481 return ret;
483 break;
486 skip1:;
488 for(i = 0; i < client->keys.len; i++) {
489 /* already added? */
490 for(j = 0; j < etypes_len; j++) {
491 if(client->keys.val[i].key.keytype == etypes[j])
492 goto skip2;
494 if (krb5_enctype_valid(context, client->keys.val[i].key.keytype) != 0)
495 continue;
496 if (!older_enctype(etypes[j]))
497 continue;
498 if (n >= pa.len)
499 krb5_abortx(context, "internal error: n >= p.len");
500 if((ret = make_etype_info_entry(context,
501 &pa.val[n++],
502 &client->keys.val[i])) != 0) {
503 free_ETYPE_INFO(&pa);
504 return ret;
506 skip2:;
509 if(n < pa.len) {
510 /* stripped out dups, newer enctypes, and not valid enctypes */
511 pa.len = n;
514 ASN1_MALLOC_ENCODE(ETYPE_INFO, buf, len, &pa, &len, ret);
515 free_ETYPE_INFO(&pa);
516 if(ret)
517 return ret;
518 ret = realloc_method_data(md);
519 if(ret) {
520 free(buf);
521 return ret;
523 md->val[md->len - 1].padata_type = KRB5_PADATA_ETYPE_INFO;
524 md->val[md->len - 1].padata_value.length = len;
525 md->val[md->len - 1].padata_value.data = buf;
526 return 0;
533 extern int _krb5_AES_string_to_default_iterator;
535 static krb5_error_code
536 make_etype_info2_entry(ETYPE_INFO2_ENTRY *ent, Key *key)
538 ent->etype = key->key.keytype;
539 if(key->salt) {
540 ALLOC(ent->salt);
541 if (ent->salt == NULL)
542 return ENOMEM;
543 *ent->salt = malloc(key->salt->salt.length + 1);
544 if (*ent->salt == NULL) {
545 free(ent->salt);
546 ent->salt = NULL;
547 return ENOMEM;
549 memcpy(*ent->salt, key->salt->salt.data, key->salt->salt.length);
550 (*ent->salt)[key->salt->salt.length] = '\0';
551 } else
552 ent->salt = NULL;
554 ent->s2kparams = NULL;
556 switch (key->key.keytype) {
557 case ETYPE_AES128_CTS_HMAC_SHA1_96:
558 case ETYPE_AES256_CTS_HMAC_SHA1_96:
559 ALLOC(ent->s2kparams);
560 if (ent->s2kparams == NULL)
561 return ENOMEM;
562 ent->s2kparams->length = 4;
563 ent->s2kparams->data = malloc(ent->s2kparams->length);
564 if (ent->s2kparams->data == NULL) {
565 free(ent->s2kparams);
566 ent->s2kparams = NULL;
567 return ENOMEM;
569 _krb5_put_int(ent->s2kparams->data,
570 _krb5_AES_string_to_default_iterator,
571 ent->s2kparams->length);
572 break;
573 case ETYPE_DES_CBC_CRC:
574 case ETYPE_DES_CBC_MD4:
575 case ETYPE_DES_CBC_MD5:
576 /* Check if this was a AFS3 salted key */
577 if(key->salt && key->salt->type == hdb_afs3_salt){
578 ALLOC(ent->s2kparams);
579 if (ent->s2kparams == NULL)
580 return ENOMEM;
581 ent->s2kparams->length = 1;
582 ent->s2kparams->data = malloc(ent->s2kparams->length);
583 if (ent->s2kparams->data == NULL) {
584 free(ent->s2kparams);
585 ent->s2kparams = NULL;
586 return ENOMEM;
588 _krb5_put_int(ent->s2kparams->data,
590 ent->s2kparams->length);
592 break;
593 default:
594 break;
596 return 0;
600 * Return an ETYPE-INFO2. Enctypes are storted the same way as in the
601 * database (client supported enctypes first, then the unsupported
602 * enctypes).
605 static krb5_error_code
606 get_pa_etype_info2(krb5_context context,
607 krb5_kdc_configuration *config,
608 METHOD_DATA *md, hdb_entry *client,
609 ENCTYPE *etypes, unsigned int etypes_len)
611 krb5_error_code ret = 0;
612 int i, j;
613 unsigned int n = 0;
614 ETYPE_INFO2 pa;
615 unsigned char *buf;
616 size_t len;
618 pa.len = client->keys.len;
619 if(pa.len > UINT_MAX/sizeof(*pa.val))
620 return ERANGE;
621 pa.val = malloc(pa.len * sizeof(*pa.val));
622 if(pa.val == NULL)
623 return ENOMEM;
624 memset(pa.val, 0, pa.len * sizeof(*pa.val));
626 for(i = 0; i < client->keys.len; i++) {
627 for (j = 0; j < n; j++)
628 if (pa.val[j].etype == client->keys.val[i].key.keytype)
629 goto skip1;
630 for(j = 0; j < etypes_len; j++) {
631 if(client->keys.val[i].key.keytype == etypes[j]) {
632 if (krb5_enctype_valid(context, etypes[j]) != 0)
633 continue;
634 if (n >= pa.len)
635 krb5_abortx(context, "internal error: n >= p.len");
636 if((ret = make_etype_info2_entry(&pa.val[n++],
637 &client->keys.val[i])) != 0) {
638 free_ETYPE_INFO2(&pa);
639 return ret;
641 break;
644 skip1:;
646 /* send enctypes that the client doesn't know about too */
647 for(i = 0; i < client->keys.len; i++) {
648 /* already added? */
649 for(j = 0; j < etypes_len; j++) {
650 if(client->keys.val[i].key.keytype == etypes[j])
651 goto skip2;
653 if (krb5_enctype_valid(context, client->keys.val[i].key.keytype) != 0)
654 continue;
655 if (n >= pa.len)
656 krb5_abortx(context, "internal error: n >= p.len");
657 if((ret = make_etype_info2_entry(&pa.val[n++],
658 &client->keys.val[i])) != 0) {
659 free_ETYPE_INFO2(&pa);
660 return ret;
662 skip2:;
665 if(n < pa.len) {
666 /* stripped out dups, and not valid enctypes */
667 pa.len = n;
670 ASN1_MALLOC_ENCODE(ETYPE_INFO2, buf, len, &pa, &len, ret);
671 free_ETYPE_INFO2(&pa);
672 if(ret)
673 return ret;
674 ret = realloc_method_data(md);
675 if(ret) {
676 free(buf);
677 return ret;
679 md->val[md->len - 1].padata_type = KRB5_PADATA_ETYPE_INFO2;
680 md->val[md->len - 1].padata_value.length = len;
681 md->val[md->len - 1].padata_value.data = buf;
682 return 0;
689 static void
690 log_as_req(krb5_context context,
691 krb5_kdc_configuration *config,
692 krb5_enctype cetype,
693 krb5_enctype setype,
694 const KDC_REQ_BODY *b)
696 krb5_error_code ret;
697 struct rk_strpool *p = NULL;
698 char *str;
699 int i;
701 for (i = 0; i < b->etype.len; i++) {
702 ret = krb5_enctype_to_string(context, b->etype.val[i], &str);
703 if (ret == 0) {
704 p = rk_strpoolprintf(p, "%s", str);
705 free(str);
706 } else
707 p = rk_strpoolprintf(p, "%d", b->etype.val[i]);
708 if (p && i + 1 < b->etype.len)
709 p = rk_strpoolprintf(p, ", ");
710 if (p == NULL) {
711 kdc_log(context, config, 0, "out of memory");
712 return;
715 if (p == NULL)
716 p = rk_strpoolprintf(p, "no encryption types");
718 str = rk_strpoolcollect(p);
719 kdc_log(context, config, 0, "Client supported enctypes: %s", str);
720 free(str);
723 char *cet;
724 char *set;
726 ret = krb5_enctype_to_string(context, cetype, &cet);
727 if(ret == 0) {
728 ret = krb5_enctype_to_string(context, setype, &set);
729 if (ret == 0) {
730 kdc_log(context, config, 5, "Using %s/%s", cet, set);
731 free(set);
733 free(cet);
735 if (ret != 0)
736 kdc_log(context, config, 5, "Using e-types %d/%d", cetype, setype);
740 char fixedstr[128];
741 unparse_flags(KDCOptions2int(b->kdc_options), asn1_KDCOptions_units(),
742 fixedstr, sizeof(fixedstr));
743 if(*fixedstr)
744 kdc_log(context, config, 2, "Requested flags: %s", fixedstr);
749 * verify the flags on `client' and `server', returning 0
750 * if they are OK and generating an error messages and returning
751 * and error code otherwise.
754 krb5_error_code
755 _kdc_check_flags(krb5_context context,
756 krb5_kdc_configuration *config,
757 hdb_entry_ex *client_ex, const char *client_name,
758 hdb_entry_ex *server_ex, const char *server_name,
759 krb5_boolean is_as_req)
761 if(client_ex != NULL) {
762 hdb_entry *client = &client_ex->entry;
764 /* check client */
765 if (client->flags.invalid) {
766 kdc_log(context, config, 0,
767 "Client (%s) has invalid bit set", client_name);
768 return KRB5KDC_ERR_POLICY;
771 if(!client->flags.client){
772 kdc_log(context, config, 0,
773 "Principal may not act as client -- %s", client_name);
774 return KRB5KDC_ERR_POLICY;
777 if (client->valid_start && *client->valid_start > kdc_time) {
778 char starttime_str[100];
779 krb5_format_time(context, *client->valid_start,
780 starttime_str, sizeof(starttime_str), TRUE);
781 kdc_log(context, config, 0,
782 "Client not yet valid until %s -- %s",
783 starttime_str, client_name);
784 return KRB5KDC_ERR_CLIENT_NOTYET;
787 if (client->valid_end && *client->valid_end < kdc_time) {
788 char endtime_str[100];
789 krb5_format_time(context, *client->valid_end,
790 endtime_str, sizeof(endtime_str), TRUE);
791 kdc_log(context, config, 0,
792 "Client expired at %s -- %s",
793 endtime_str, client_name);
794 return KRB5KDC_ERR_NAME_EXP;
797 if (client->pw_end && *client->pw_end < kdc_time
798 && (server_ex == NULL || !server_ex->entry.flags.change_pw)) {
799 char pwend_str[100];
800 krb5_format_time(context, *client->pw_end,
801 pwend_str, sizeof(pwend_str), TRUE);
802 kdc_log(context, config, 0,
803 "Client's key has expired at %s -- %s",
804 pwend_str, client_name);
805 return KRB5KDC_ERR_KEY_EXPIRED;
809 /* check server */
811 if (server_ex != NULL) {
812 hdb_entry *server = &server_ex->entry;
814 if (server->flags.invalid) {
815 kdc_log(context, config, 0,
816 "Server has invalid flag set -- %s", server_name);
817 return KRB5KDC_ERR_POLICY;
820 if(!server->flags.server){
821 kdc_log(context, config, 0,
822 "Principal may not act as server -- %s", server_name);
823 return KRB5KDC_ERR_POLICY;
826 if(!is_as_req && server->flags.initial) {
827 kdc_log(context, config, 0,
828 "AS-REQ is required for server -- %s", server_name);
829 return KRB5KDC_ERR_POLICY;
832 if (server->valid_start && *server->valid_start > kdc_time) {
833 char starttime_str[100];
834 krb5_format_time(context, *server->valid_start,
835 starttime_str, sizeof(starttime_str), TRUE);
836 kdc_log(context, config, 0,
837 "Server not yet valid until %s -- %s",
838 starttime_str, server_name);
839 return KRB5KDC_ERR_SERVICE_NOTYET;
842 if (server->valid_end && *server->valid_end < kdc_time) {
843 char endtime_str[100];
844 krb5_format_time(context, *server->valid_end,
845 endtime_str, sizeof(endtime_str), TRUE);
846 kdc_log(context, config, 0,
847 "Server expired at %s -- %s",
848 endtime_str, server_name);
849 return KRB5KDC_ERR_SERVICE_EXP;
852 if (server->pw_end && *server->pw_end < kdc_time) {
853 char pwend_str[100];
854 krb5_format_time(context, *server->pw_end,
855 pwend_str, sizeof(pwend_str), TRUE);
856 kdc_log(context, config, 0,
857 "Server's key has expired at -- %s",
858 pwend_str, server_name);
859 return KRB5KDC_ERR_KEY_EXPIRED;
862 return 0;
866 * Return TRUE if `from' is part of `addresses' taking into consideration
867 * the configuration variables that tells us how strict we should be about
868 * these checks
871 krb5_boolean
872 _kdc_check_addresses(krb5_context context,
873 krb5_kdc_configuration *config,
874 HostAddresses *addresses, const struct sockaddr *from)
876 krb5_error_code ret;
877 krb5_address addr;
878 krb5_boolean result;
879 krb5_boolean only_netbios = TRUE;
880 int i;
882 if(config->check_ticket_addresses == 0)
883 return TRUE;
885 if(addresses == NULL)
886 return config->allow_null_ticket_addresses;
888 for (i = 0; i < addresses->len; ++i) {
889 if (addresses->val[i].addr_type != KRB5_ADDRESS_NETBIOS) {
890 only_netbios = FALSE;
894 /* Windows sends it's netbios name, which I can only assume is
895 * used for the 'allowed workstations' check. This is painful,
896 * but we still want to check IP addresses if they happen to be
897 * present.
900 if(only_netbios)
901 return config->allow_null_ticket_addresses;
903 ret = krb5_sockaddr2address (context, from, &addr);
904 if(ret)
905 return FALSE;
907 result = krb5_address_search(context, &addr, addresses);
908 krb5_free_address (context, &addr);
909 return result;
916 static krb5_boolean
917 send_pac_p(krb5_context context, KDC_REQ *req)
919 krb5_error_code ret;
920 PA_PAC_REQUEST pacreq;
921 const PA_DATA *pa;
922 int i = 0;
924 pa = _kdc_find_padata(req, &i, KRB5_PADATA_PA_PAC_REQUEST);
925 if (pa == NULL)
926 return TRUE;
928 ret = decode_PA_PAC_REQUEST(pa->padata_value.data,
929 pa->padata_value.length,
930 &pacreq,
931 NULL);
932 if (ret)
933 return TRUE;
934 i = pacreq.include_pac;
935 free_PA_PAC_REQUEST(&pacreq);
936 if (i == 0)
937 return FALSE;
938 return TRUE;
945 krb5_error_code
946 _kdc_as_rep(krb5_context context,
947 krb5_kdc_configuration *config,
948 KDC_REQ *req,
949 const krb5_data *req_buffer,
950 krb5_data *reply,
951 const char *from,
952 struct sockaddr *from_addr,
953 int datagram_reply)
955 KDC_REQ_BODY *b = &req->req_body;
956 AS_REP rep;
957 KDCOptions f = b->kdc_options;
958 hdb_entry_ex *client = NULL, *server = NULL;
959 krb5_enctype cetype, setype, sessionetype;
960 krb5_data e_data;
961 EncTicketPart et;
962 EncKDCRepPart ek;
963 krb5_principal client_princ = NULL, server_princ = NULL;
964 char *client_name = NULL, *server_name = NULL;
965 krb5_error_code ret = 0;
966 const char *e_text = NULL;
967 krb5_crypto crypto;
968 Key *ckey, *skey;
969 EncryptionKey *reply_key;
970 int flags = 0;
971 #ifdef PKINIT
972 pk_client_params *pkp = NULL;
973 #endif
975 memset(&rep, 0, sizeof(rep));
976 krb5_data_zero(&e_data);
978 if (f.canonicalize)
979 flags |= HDB_F_CANON;
981 if(b->sname == NULL){
982 ret = KRB5KRB_ERR_GENERIC;
983 e_text = "No server in request";
984 } else{
985 ret = _krb5_principalname2krb5_principal (context,
986 &server_princ,
987 *(b->sname),
988 b->realm);
989 if (ret == 0)
990 ret = krb5_unparse_name(context, server_princ, &server_name);
992 if (ret) {
993 kdc_log(context, config, 0,
994 "AS-REQ malformed server name from %s", from);
995 goto out;
998 if(b->cname == NULL){
999 ret = KRB5KRB_ERR_GENERIC;
1000 e_text = "No client in request";
1001 } else {
1003 if (b->cname->name_type == KRB5_NT_ENTERPRISE_PRINCIPAL) {
1004 if (b->cname->name_string.len != 1) {
1005 kdc_log(context, config, 0,
1006 "AS-REQ malformed canon request from %s, "
1007 "enterprise name with %d name components",
1008 from, b->cname->name_string.len);
1009 ret = KRB5_PARSE_MALFORMED;
1010 goto out;
1012 ret = krb5_parse_name(context, b->cname->name_string.val[0],
1013 &client_princ);
1014 if (ret)
1015 goto out;
1016 } else {
1017 ret = _krb5_principalname2krb5_principal (context,
1018 &client_princ,
1019 *(b->cname),
1020 b->realm);
1021 if (ret)
1022 goto out;
1024 ret = krb5_unparse_name(context, client_princ, &client_name);
1026 if (ret) {
1027 kdc_log(context, config, 0,
1028 "AS-REQ malformed client name from %s", from);
1029 goto out;
1032 kdc_log(context, config, 0, "AS-REQ %s from %s for %s",
1033 client_name, from, server_name);
1035 ret = _kdc_db_fetch(context, config, client_princ,
1036 HDB_F_GET_CLIENT | flags, NULL, &client);
1037 if(ret){
1038 kdc_log(context, config, 0, "UNKNOWN -- %s: %s", client_name,
1039 krb5_get_err_text(context, ret));
1040 ret = KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN;
1041 goto out;
1044 ret = _kdc_db_fetch(context, config, server_princ,
1045 HDB_F_GET_SERVER|HDB_F_GET_KRBTGT,
1046 NULL, &server);
1047 if(ret){
1048 kdc_log(context, config, 0, "UNKNOWN -- %s: %s", server_name,
1049 krb5_get_err_text(context, ret));
1050 ret = KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN;
1051 goto out;
1054 ret = _kdc_windc_client_access(context, client, req);
1055 if(ret)
1056 goto out;
1058 ret = _kdc_check_flags(context, config,
1059 client, client_name,
1060 server, server_name,
1061 TRUE);
1062 if(ret)
1063 goto out;
1065 memset(&et, 0, sizeof(et));
1066 memset(&ek, 0, sizeof(ek));
1068 if(req->padata){
1069 int i;
1070 const PA_DATA *pa;
1071 int found_pa = 0;
1073 log_patypes(context, config, req->padata);
1075 #ifdef PKINIT
1076 kdc_log(context, config, 5,
1077 "Looking for PKINIT pa-data -- %s", client_name);
1079 e_text = "No PKINIT PA found";
1081 i = 0;
1082 if ((pa = _kdc_find_padata(req, &i, KRB5_PADATA_PK_AS_REQ)))
1084 if (pa == NULL) {
1085 i = 0;
1086 if((pa = _kdc_find_padata(req, &i, KRB5_PADATA_PK_AS_REQ_WIN)))
1089 if (pa) {
1090 char *client_cert = NULL;
1092 ret = _kdc_pk_rd_padata(context, config, req, pa, &pkp);
1093 if (ret) {
1094 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
1095 kdc_log(context, config, 5,
1096 "Failed to decode PKINIT PA-DATA -- %s",
1097 client_name);
1098 goto ts_enc;
1100 if (ret == 0 && pkp == NULL)
1101 goto ts_enc;
1103 ret = _kdc_pk_check_client(context,
1104 config,
1105 client,
1106 pkp,
1107 &client_cert);
1108 if (ret) {
1109 e_text = "PKINIT certificate not allowed to "
1110 "impersonate principal";
1111 _kdc_pk_free_client_param(context, pkp);
1113 kdc_log(context, config, 0, "%s", e_text);
1114 pkp = NULL;
1115 goto out;
1117 found_pa = 1;
1118 et.flags.pre_authent = 1;
1119 kdc_log(context, config, 0,
1120 "PKINIT pre-authentication succeeded -- %s using %s",
1121 client_name, client_cert);
1122 free(client_cert);
1123 if (pkp)
1124 goto preauth_done;
1126 ts_enc:
1127 #endif
1128 kdc_log(context, config, 5, "Looking for ENC-TS pa-data -- %s",
1129 client_name);
1131 i = 0;
1132 e_text = "No ENC-TS found";
1133 while((pa = _kdc_find_padata(req, &i, KRB5_PADATA_ENC_TIMESTAMP))){
1134 krb5_data ts_data;
1135 PA_ENC_TS_ENC p;
1136 size_t len;
1137 EncryptedData enc_data;
1138 Key *pa_key;
1139 char *str;
1141 found_pa = 1;
1143 ret = decode_EncryptedData(pa->padata_value.data,
1144 pa->padata_value.length,
1145 &enc_data,
1146 &len);
1147 if (ret) {
1148 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
1149 kdc_log(context, config, 5, "Failed to decode PA-DATA -- %s",
1150 client_name);
1151 goto out;
1154 ret = hdb_enctype2key(context, &client->entry,
1155 enc_data.etype, &pa_key);
1156 if(ret){
1157 char *estr;
1158 e_text = "No key matches pa-data";
1159 ret = KRB5KDC_ERR_ETYPE_NOSUPP;
1160 if(krb5_enctype_to_string(context, enc_data.etype, &estr))
1161 estr = NULL;
1162 if(estr == NULL)
1163 kdc_log(context, config, 5,
1164 "No client key matching pa-data (%d) -- %s",
1165 enc_data.etype, client_name);
1166 else
1167 kdc_log(context, config, 5,
1168 "No client key matching pa-data (%s) -- %s",
1169 estr, client_name);
1170 free(estr);
1172 free_EncryptedData(&enc_data);
1173 continue;
1176 try_next_key:
1177 ret = krb5_crypto_init(context, &pa_key->key, 0, &crypto);
1178 if (ret) {
1179 kdc_log(context, config, 0, "krb5_crypto_init failed: %s",
1180 krb5_get_err_text(context, ret));
1181 free_EncryptedData(&enc_data);
1182 continue;
1185 ret = krb5_decrypt_EncryptedData (context,
1186 crypto,
1187 KRB5_KU_PA_ENC_TIMESTAMP,
1188 &enc_data,
1189 &ts_data);
1190 krb5_crypto_destroy(context, crypto);
1191 if(ret){
1192 krb5_error_code ret2;
1193 ret2 = krb5_enctype_to_string(context,
1194 pa_key->key.keytype, &str);
1195 if (ret2)
1196 str = NULL;
1197 kdc_log(context, config, 5,
1198 "Failed to decrypt PA-DATA -- %s "
1199 "(enctype %s) error %s",
1200 client_name,
1201 str ? str : "unknown enctype",
1202 krb5_get_err_text(context, ret));
1203 free(str);
1205 if(hdb_next_enctype2key(context, &client->entry,
1206 enc_data.etype, &pa_key) == 0)
1207 goto try_next_key;
1208 e_text = "Failed to decrypt PA-DATA";
1210 free_EncryptedData(&enc_data);
1211 ret = KRB5KDC_ERR_PREAUTH_FAILED;
1212 continue;
1214 free_EncryptedData(&enc_data);
1215 ret = decode_PA_ENC_TS_ENC(ts_data.data,
1216 ts_data.length,
1218 &len);
1219 krb5_data_free(&ts_data);
1220 if(ret){
1221 e_text = "Failed to decode PA-ENC-TS-ENC";
1222 ret = KRB5KDC_ERR_PREAUTH_FAILED;
1223 kdc_log(context, config,
1224 5, "Failed to decode PA-ENC-TS_ENC -- %s",
1225 client_name);
1226 continue;
1228 free_PA_ENC_TS_ENC(&p);
1229 if (abs(kdc_time - p.patimestamp) > context->max_skew) {
1230 char client_time[100];
1232 krb5_format_time(context, p.patimestamp,
1233 client_time, sizeof(client_time), TRUE);
1235 ret = KRB5KRB_AP_ERR_SKEW;
1236 kdc_log(context, config, 0,
1237 "Too large time skew, "
1238 "client time %s is out by %u > %u seconds -- %s",
1239 client_time,
1240 (unsigned)abs(kdc_time - p.patimestamp),
1241 context->max_skew,
1242 client_name);
1243 #if 0
1244 /* This code is from samba, needs testing */
1246 * the following is needed to make windows clients
1247 * to retry using the timestamp in the error message
1249 * this is maybe a bug in windows to not trying when e_text
1250 * is present...
1252 e_text = NULL;
1253 #else
1254 e_text = "Too large time skew";
1255 #endif
1256 goto out;
1258 et.flags.pre_authent = 1;
1260 ret = krb5_enctype_to_string(context,pa_key->key.keytype, &str);
1261 if (ret)
1262 str = NULL;
1264 kdc_log(context, config, 2,
1265 "ENC-TS Pre-authentication succeeded -- %s using %s",
1266 client_name, str ? str : "unknown enctype");
1267 free(str);
1268 break;
1270 #ifdef PKINIT
1271 preauth_done:
1272 #endif
1273 if(found_pa == 0 && config->require_preauth)
1274 goto use_pa;
1275 /* We come here if we found a pa-enc-timestamp, but if there
1276 was some problem with it, other than too large skew */
1277 if(found_pa && et.flags.pre_authent == 0){
1278 kdc_log(context, config, 0, "%s -- %s", e_text, client_name);
1279 e_text = NULL;
1280 goto out;
1282 }else if (config->require_preauth
1283 || client->entry.flags.require_preauth
1284 || server->entry.flags.require_preauth) {
1285 METHOD_DATA method_data;
1286 PA_DATA *pa;
1287 unsigned char *buf;
1288 size_t len;
1290 use_pa:
1291 method_data.len = 0;
1292 method_data.val = NULL;
1294 ret = realloc_method_data(&method_data);
1295 pa = &method_data.val[method_data.len-1];
1296 pa->padata_type = KRB5_PADATA_ENC_TIMESTAMP;
1297 pa->padata_value.length = 0;
1298 pa->padata_value.data = NULL;
1300 #ifdef PKINIT
1301 ret = realloc_method_data(&method_data);
1302 pa = &method_data.val[method_data.len-1];
1303 pa->padata_type = KRB5_PADATA_PK_AS_REQ;
1304 pa->padata_value.length = 0;
1305 pa->padata_value.data = NULL;
1307 ret = realloc_method_data(&method_data);
1308 pa = &method_data.val[method_data.len-1];
1309 pa->padata_type = KRB5_PADATA_PK_AS_REQ_WIN;
1310 pa->padata_value.length = 0;
1311 pa->padata_value.data = NULL;
1312 #endif
1315 * RFC4120 requires:
1316 * - If the client only knows about old enctypes, then send
1317 * both info replies (we send 'info' first in the list).
1318 * - If the client is 'modern', because it knows about 'new'
1319 * enctype types, then only send the 'info2' reply.
1322 /* XXX check ret */
1323 if (only_older_enctype_p(req))
1324 ret = get_pa_etype_info(context, config,
1325 &method_data, &client->entry,
1326 b->etype.val, b->etype.len);
1327 /* XXX check ret */
1328 ret = get_pa_etype_info2(context, config, &method_data,
1329 &client->entry, b->etype.val, b->etype.len);
1332 ASN1_MALLOC_ENCODE(METHOD_DATA, buf, len, &method_data, &len, ret);
1333 free_METHOD_DATA(&method_data);
1335 e_data.data = buf;
1336 e_data.length = len;
1337 e_text ="Need to use PA-ENC-TIMESTAMP/PA-PK-AS-REQ",
1339 ret = KRB5KDC_ERR_PREAUTH_REQUIRED;
1341 kdc_log(context, config, 0,
1342 "No preauth found, returning PREAUTH-REQUIRED -- %s",
1343 client_name);
1344 goto out;
1348 * Find the client key (for preauth ENC-TS verification and reply
1349 * encryption). Then the best encryption type for the KDC and
1350 * last the best session key that shared between the client and
1351 * KDC runtime enctypes.
1354 ret = _kdc_find_etype(context, client, b->etype.val, b->etype.len,
1355 &ckey, &cetype);
1356 if (ret) {
1357 kdc_log(context, config, 0,
1358 "Client (%s) has no support for etypes", client_name);
1359 goto out;
1362 ret = _kdc_get_preferred_key(context, config,
1363 server, server_name,
1364 &setype, &skey);
1365 if(ret)
1366 goto out;
1369 * Select a session enctype from the list of the crypto systems
1370 * supported enctype, is supported by the client and is one of the
1371 * enctype of the enctype of the krbtgt.
1373 * The later is used as a hint what enctype all KDC are supporting
1374 * to make sure a newer version of KDC wont generate a session
1375 * enctype that and older version of a KDC in the same realm can't
1376 * decrypt.
1378 * But if the KDC admin is paranoid and doesn't want to have "no
1379 * the best" enctypes on the krbtgt, lets save the best pick from
1380 * the client list and hope that that will work for any other
1381 * KDCs.
1384 const krb5_enctype *p;
1385 krb5_enctype clientbest = ETYPE_NULL;
1386 int i, j;
1388 p = krb5_kerberos_enctypes(context);
1390 sessionetype = ETYPE_NULL;
1392 for (i = 0; p[i] != ETYPE_NULL && sessionetype == ETYPE_NULL; i++) {
1393 if (krb5_enctype_valid(context, p[i]) != 0)
1394 continue;
1396 for (j = 0; j < b->etype.len && sessionetype == ETYPE_NULL; j++) {
1397 Key *dummy;
1398 /* check with client */
1399 if (p[i] != b->etype.val[j])
1400 continue;
1401 /* save best of union of { client, crypto system } */
1402 if (clientbest == ETYPE_NULL)
1403 clientbest = p[i];
1404 /* check with krbtgt */
1405 ret = hdb_enctype2key(context, &server->entry, p[i], &dummy);
1406 if (ret)
1407 continue;
1408 sessionetype = p[i];
1411 /* if krbtgt had no shared keys with client, pick clients best */
1412 if (clientbest != ETYPE_NULL && sessionetype == ETYPE_NULL) {
1413 sessionetype = clientbest;
1414 } else if (sessionetype == ETYPE_NULL) {
1415 kdc_log(context, config, 0,
1416 "Client (%s) from %s has no common enctypes with KDC"
1417 "to use for the session key",
1418 client_name, from);
1419 goto out;
1423 log_as_req(context, config, cetype, setype, b);
1425 if(f.renew || f.validate || f.proxy || f.forwarded || f.enc_tkt_in_skey
1426 || (f.request_anonymous && !config->allow_anonymous)) {
1427 ret = KRB5KDC_ERR_BADOPTION;
1428 kdc_log(context, config, 0, "Bad KDC options -- %s", client_name);
1429 goto out;
1432 rep.pvno = 5;
1433 rep.msg_type = krb_as_rep;
1434 copy_Realm(&client->entry.principal->realm, &rep.crealm);
1435 if (f.request_anonymous)
1436 _kdc_make_anonymous_principalname (&rep.cname);
1437 else
1438 _krb5_principal2principalname(&rep.cname,
1439 client->entry.principal);
1440 rep.ticket.tkt_vno = 5;
1441 copy_Realm(&server->entry.principal->realm, &rep.ticket.realm);
1442 _krb5_principal2principalname(&rep.ticket.sname,
1443 server->entry.principal);
1444 /* java 1.6 expects the name to be the same type, lets allow that
1445 * uncomplicated name-types. */
1446 #define CNT(sp,t) (((sp)->sname->name_type) == KRB5_NT_##t)
1447 if (CNT(b, UNKNOWN) || CNT(b, PRINCIPAL) || CNT(b, SRV_INST) || CNT(b, SRV_HST) || CNT(b, SRV_XHST))
1448 rep.ticket.sname.name_type = b->sname->name_type;
1449 #undef CNT
1451 et.flags.initial = 1;
1452 if(client->entry.flags.forwardable && server->entry.flags.forwardable)
1453 et.flags.forwardable = f.forwardable;
1454 else if (f.forwardable) {
1455 ret = KRB5KDC_ERR_POLICY;
1456 kdc_log(context, config, 0,
1457 "Ticket may not be forwardable -- %s", client_name);
1458 goto out;
1460 if(client->entry.flags.proxiable && server->entry.flags.proxiable)
1461 et.flags.proxiable = f.proxiable;
1462 else if (f.proxiable) {
1463 ret = KRB5KDC_ERR_POLICY;
1464 kdc_log(context, config, 0,
1465 "Ticket may not be proxiable -- %s", client_name);
1466 goto out;
1468 if(client->entry.flags.postdate && server->entry.flags.postdate)
1469 et.flags.may_postdate = f.allow_postdate;
1470 else if (f.allow_postdate){
1471 ret = KRB5KDC_ERR_POLICY;
1472 kdc_log(context, config, 0,
1473 "Ticket may not be postdatable -- %s", client_name);
1474 goto out;
1477 /* check for valid set of addresses */
1478 if(!_kdc_check_addresses(context, config, b->addresses, from_addr)) {
1479 ret = KRB5KRB_AP_ERR_BADADDR;
1480 kdc_log(context, config, 0,
1481 "Bad address list requested -- %s", client_name);
1482 goto out;
1485 ret = krb5_generate_random_keyblock(context, sessionetype, &et.key);
1486 if (ret)
1487 goto out;
1488 copy_PrincipalName(&rep.cname, &et.cname);
1489 copy_Realm(&rep.crealm, &et.crealm);
1492 time_t start;
1493 time_t t;
1495 start = et.authtime = kdc_time;
1497 if(f.postdated && req->req_body.from){
1498 ALLOC(et.starttime);
1499 start = *et.starttime = *req->req_body.from;
1500 et.flags.invalid = 1;
1501 et.flags.postdated = 1; /* XXX ??? */
1503 _kdc_fix_time(&b->till);
1504 t = *b->till;
1506 /* be careful not overflowing */
1508 if(client->entry.max_life)
1509 t = start + min(t - start, *client->entry.max_life);
1510 if(server->entry.max_life)
1511 t = start + min(t - start, *server->entry.max_life);
1512 #if 0
1513 t = min(t, start + realm->max_life);
1514 #endif
1515 et.endtime = t;
1516 if(f.renewable_ok && et.endtime < *b->till){
1517 f.renewable = 1;
1518 if(b->rtime == NULL){
1519 ALLOC(b->rtime);
1520 *b->rtime = 0;
1522 if(*b->rtime < *b->till)
1523 *b->rtime = *b->till;
1525 if(f.renewable && b->rtime){
1526 t = *b->rtime;
1527 if(t == 0)
1528 t = MAX_TIME;
1529 if(client->entry.max_renew)
1530 t = start + min(t - start, *client->entry.max_renew);
1531 if(server->entry.max_renew)
1532 t = start + min(t - start, *server->entry.max_renew);
1533 #if 0
1534 t = min(t, start + realm->max_renew);
1535 #endif
1536 ALLOC(et.renew_till);
1537 *et.renew_till = t;
1538 et.flags.renewable = 1;
1542 if (f.request_anonymous)
1543 et.flags.anonymous = 1;
1545 if(b->addresses){
1546 ALLOC(et.caddr);
1547 copy_HostAddresses(b->addresses, et.caddr);
1550 et.transited.tr_type = DOMAIN_X500_COMPRESS;
1551 krb5_data_zero(&et.transited.contents);
1553 copy_EncryptionKey(&et.key, &ek.key);
1555 /* The MIT ASN.1 library (obviously) doesn't tell lengths encoded
1556 * as 0 and as 0x80 (meaning indefinite length) apart, and is thus
1557 * incapable of correctly decoding SEQUENCE OF's of zero length.
1559 * To fix this, always send at least one no-op last_req
1561 * If there's a pw_end or valid_end we will use that,
1562 * otherwise just a dummy lr.
1564 ek.last_req.val = malloc(2 * sizeof(*ek.last_req.val));
1565 if (ek.last_req.val == NULL) {
1566 ret = ENOMEM;
1567 goto out;
1569 ek.last_req.len = 0;
1570 if (client->entry.pw_end
1571 && (config->kdc_warn_pwexpire == 0
1572 || kdc_time + config->kdc_warn_pwexpire >= *client->entry.pw_end)) {
1573 ek.last_req.val[ek.last_req.len].lr_type = LR_PW_EXPTIME;
1574 ek.last_req.val[ek.last_req.len].lr_value = *client->entry.pw_end;
1575 ++ek.last_req.len;
1577 if (client->entry.valid_end) {
1578 ek.last_req.val[ek.last_req.len].lr_type = LR_ACCT_EXPTIME;
1579 ek.last_req.val[ek.last_req.len].lr_value = *client->entry.valid_end;
1580 ++ek.last_req.len;
1582 if (ek.last_req.len == 0) {
1583 ek.last_req.val[ek.last_req.len].lr_type = LR_NONE;
1584 ek.last_req.val[ek.last_req.len].lr_value = 0;
1585 ++ek.last_req.len;
1587 ek.nonce = b->nonce;
1588 if (client->entry.valid_end || client->entry.pw_end) {
1589 ALLOC(ek.key_expiration);
1590 if (client->entry.valid_end) {
1591 if (client->entry.pw_end)
1592 *ek.key_expiration = min(*client->entry.valid_end,
1593 *client->entry.pw_end);
1594 else
1595 *ek.key_expiration = *client->entry.valid_end;
1596 } else
1597 *ek.key_expiration = *client->entry.pw_end;
1598 } else
1599 ek.key_expiration = NULL;
1600 ek.flags = et.flags;
1601 ek.authtime = et.authtime;
1602 if (et.starttime) {
1603 ALLOC(ek.starttime);
1604 *ek.starttime = *et.starttime;
1606 ek.endtime = et.endtime;
1607 if (et.renew_till) {
1608 ALLOC(ek.renew_till);
1609 *ek.renew_till = *et.renew_till;
1611 copy_Realm(&rep.ticket.realm, &ek.srealm);
1612 copy_PrincipalName(&rep.ticket.sname, &ek.sname);
1613 if(et.caddr){
1614 ALLOC(ek.caddr);
1615 copy_HostAddresses(et.caddr, ek.caddr);
1618 ALLOC(rep.padata);
1619 rep.padata->len = 0;
1620 rep.padata->val = NULL;
1622 reply_key = &ckey->key;
1623 #if PKINIT
1624 if (pkp) {
1625 ret = _kdc_pk_mk_pa_reply(context, config, pkp, client,
1626 req, req_buffer,
1627 &reply_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 #endif
1639 set_salt_padata (rep.padata, ckey->salt);
1641 /* Add signing of alias referral */
1642 if (f.canonicalize) {
1643 PA_ClientCanonicalized canon;
1644 krb5_data data;
1645 PA_DATA pa;
1646 krb5_crypto crypto;
1647 size_t len;
1649 memset(&canon, 0, sizeof(canon));
1651 canon.names.requested_name = *b->cname;
1652 canon.names.real_name = client->entry.principal->name;
1654 ASN1_MALLOC_ENCODE(PA_ClientCanonicalizedNames, data.data, data.length,
1655 &canon.names, &len, ret);
1656 if (ret)
1657 goto out;
1658 if (data.length != len)
1659 krb5_abortx(context, "internal asn.1 error");
1661 /* sign using "returned session key" */
1662 ret = krb5_crypto_init(context, &et.key, 0, &crypto);
1663 if (ret) {
1664 free(data.data);
1665 goto out;
1668 ret = krb5_create_checksum(context, crypto,
1669 KRB5_KU_CANONICALIZED_NAMES, 0,
1670 data.data, data.length,
1671 &canon.canon_checksum);
1672 free(data.data);
1673 krb5_crypto_destroy(context, crypto);
1674 if (ret)
1675 goto out;
1677 ASN1_MALLOC_ENCODE(PA_ClientCanonicalized, data.data, data.length,
1678 &canon, &len, ret);
1679 free_Checksum(&canon.canon_checksum);
1680 if (ret)
1681 goto out;
1682 if (data.length != len)
1683 krb5_abortx(context, "internal asn.1 error");
1685 pa.padata_type = KRB5_PADATA_CLIENT_CANONICALIZED;
1686 pa.padata_value = data;
1687 ret = add_METHOD_DATA(rep.padata, &pa);
1688 free(data.data);
1689 if (ret)
1690 goto out;
1693 if (rep.padata->len == 0) {
1694 free(rep.padata);
1695 rep.padata = NULL;
1698 /* Add the PAC */
1699 if (send_pac_p(context, req)) {
1700 krb5_pac p = NULL;
1701 krb5_data data;
1703 ret = _kdc_pac_generate(context, client, &p);
1704 if (ret) {
1705 kdc_log(context, config, 0, "PAC generation failed for -- %s",
1706 client_name);
1707 goto out;
1709 if (p != NULL) {
1710 ret = _krb5_pac_sign(context, p, et.authtime,
1711 client->entry.principal,
1712 &skey->key, /* Server key */
1713 &skey->key, /* FIXME: should be krbtgt key */
1714 &data);
1715 krb5_pac_free(context, p);
1716 if (ret) {
1717 kdc_log(context, config, 0, "PAC signing failed for -- %s",
1718 client_name);
1719 goto out;
1722 ret = _kdc_tkt_add_if_relevant_ad(context, &et,
1723 KRB5_AUTHDATA_WIN2K_PAC,
1724 &data);
1725 krb5_data_free(&data);
1726 if (ret)
1727 goto out;
1731 _kdc_log_timestamp(context, config, "AS-REQ", et.authtime, et.starttime,
1732 et.endtime, et.renew_till);
1734 /* do this as the last thing since this signs the EncTicketPart */
1735 ret = _kdc_add_KRB5SignedPath(context,
1736 config,
1737 server,
1738 setype,
1739 NULL,
1740 NULL,
1741 &et);
1742 if (ret)
1743 goto out;
1745 ret = _kdc_encode_reply(context, config,
1746 &rep, &et, &ek, setype, server->entry.kvno,
1747 &skey->key, client->entry.kvno,
1748 reply_key, &e_text, reply);
1749 free_EncTicketPart(&et);
1750 free_EncKDCRepPart(&ek);
1751 if (ret)
1752 goto out;
1754 /* */
1755 if (datagram_reply && reply->length > config->max_datagram_reply_length) {
1756 krb5_data_free(reply);
1757 ret = KRB5KRB_ERR_RESPONSE_TOO_BIG;
1758 e_text = "Reply packet too large";
1761 out:
1762 free_AS_REP(&rep);
1763 if(ret){
1764 krb5_mk_error(context,
1765 ret,
1766 e_text,
1767 (e_data.data ? &e_data : NULL),
1768 client_princ,
1769 server_princ,
1770 NULL,
1771 NULL,
1772 reply);
1773 ret = 0;
1775 #ifdef PKINIT
1776 if (pkp)
1777 _kdc_pk_free_client_param(context, pkp);
1778 #endif
1779 if (e_data.data)
1780 free(e_data.data);
1781 if (client_princ)
1782 krb5_free_principal(context, client_princ);
1783 free(client_name);
1784 if (server_princ)
1785 krb5_free_principal(context, server_princ);
1786 free(server_name);
1787 if(client)
1788 _kdc_free_ent(context, client);
1789 if(server)
1790 _kdc_free_ent(context, server);
1791 return ret;
1795 * Add the AuthorizationData `data´ of `type´ to the last element in
1796 * the sequence of authorization_data in `tkt´ wrapped in an IF_RELEVANT
1799 krb5_error_code
1800 _kdc_tkt_add_if_relevant_ad(krb5_context context,
1801 EncTicketPart *tkt,
1802 int type,
1803 const krb5_data *data)
1805 krb5_error_code ret;
1806 size_t size;
1808 if (tkt->authorization_data == NULL) {
1809 tkt->authorization_data = calloc(1, sizeof(*tkt->authorization_data));
1810 if (tkt->authorization_data == NULL) {
1811 krb5_set_error_string(context, "out of memory");
1812 return ENOMEM;
1816 /* add the entry to the last element */
1818 AuthorizationData ad = { 0, NULL };
1819 AuthorizationDataElement ade;
1821 ade.ad_type = type;
1822 ade.ad_data = *data;
1824 ret = add_AuthorizationData(&ad, &ade);
1825 if (ret) {
1826 krb5_set_error_string(context, "add AuthorizationData failed");
1827 return ret;
1830 ade.ad_type = KRB5_AUTHDATA_IF_RELEVANT;
1832 ASN1_MALLOC_ENCODE(AuthorizationData,
1833 ade.ad_data.data, ade.ad_data.length,
1834 &ad, &size, ret);
1835 free_AuthorizationData(&ad);
1836 if (ret) {
1837 krb5_set_error_string(context, "ASN.1 encode of "
1838 "AuthorizationData failed");
1839 return ret;
1841 if (ade.ad_data.length != size)
1842 krb5_abortx(context, "internal asn.1 encoder error");
1844 ret = add_AuthorizationData(tkt->authorization_data, &ade);
1845 der_free_octet_string(&ade.ad_data);
1846 if (ret) {
1847 krb5_set_error_string(context, "add AuthorizationData failed");
1848 return ret;
1852 return 0;