Fix broken qop.5 man page generation
[heimdal.git] / kdc / kerberos5.c
blobbe900a25cd6fc68612cc5fcc97dfc60f598075ad
1 /*
2 * Copyright (c) 1997-2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #include "kdc_locl.h"
36 #define MAX_TIME ((time_t)((1U << 31) - 1))
38 void
39 _kdc_fix_time(time_t **t)
41 if(*t == NULL){
42 ALLOC(*t);
43 **t = MAX_TIME;
45 if(**t == 0) **t = MAX_TIME; /* fix for old clients */
48 static int
49 realloc_method_data(METHOD_DATA *md)
51 PA_DATA *pa;
52 pa = realloc(md->val, (md->len + 1) * sizeof(*md->val));
53 if(pa == NULL)
54 return ENOMEM;
55 md->val = pa;
56 md->len++;
57 return 0;
60 static void
61 set_salt_padata(METHOD_DATA *md, Salt *salt)
63 if (salt) {
64 realloc_method_data(md);
65 md->val[md->len - 1].padata_type = salt->type;
66 der_copy_octet_string(&salt->salt,
67 &md->val[md->len - 1].padata_value);
71 const PA_DATA*
72 _kdc_find_padata(const KDC_REQ *req, int *start, int type)
74 if (req->padata == NULL)
75 return NULL;
77 while((size_t)*start < req->padata->len){
78 (*start)++;
79 if(req->padata->val[*start - 1].padata_type == (unsigned)type)
80 return &req->padata->val[*start - 1];
82 return NULL;
86 * This is a hack to allow predefined weak services, like afs to
87 * still use weak types
90 krb5_boolean
91 _kdc_is_weak_exception(krb5_principal principal, krb5_enctype etype)
93 if (principal->name.name_string.len > 0 &&
94 strcmp(principal->name.name_string.val[0], "afs") == 0 &&
95 (etype == (krb5_enctype)ETYPE_DES_CBC_CRC
96 || etype == (krb5_enctype)ETYPE_DES_CBC_MD4
97 || etype == (krb5_enctype)ETYPE_DES_CBC_MD5))
98 return TRUE;
99 return FALSE;
104 * Detect if `key' is the using the the precomputed `default_salt'.
107 static krb5_boolean
108 is_default_salt_p(const krb5_salt *default_salt, const Key *key)
110 if (key->salt == NULL)
111 return TRUE;
112 if (default_salt->salttype != key->salt->type)
113 return FALSE;
114 if (krb5_data_cmp(&default_salt->saltvalue, &key->salt->salt))
115 return FALSE;
116 return TRUE;
120 * return the first appropriate key of `princ' in `ret_key'. Look for
121 * all the etypes in (`etypes', `len'), stopping as soon as we find
122 * one, but preferring one that has default salt
125 krb5_error_code
126 _kdc_find_etype(krb5_context context, krb5_boolean use_strongest_session_key,
127 krb5_boolean is_preauth, hdb_entry_ex *princ,
128 krb5_enctype *etypes, unsigned len,
129 krb5_enctype *ret_enctype, Key **ret_key)
131 krb5_error_code ret;
132 krb5_salt def_salt;
133 krb5_enctype enctype = ETYPE_NULL;
134 Key *key;
135 int i;
137 /* We'll want to avoid keys with v4 salted keys in the pre-auth case... */
138 ret = krb5_get_pw_salt(context, princ->entry.principal, &def_salt);
139 if (ret)
140 return ret;
142 ret = KRB5KDC_ERR_ETYPE_NOSUPP;
144 if (use_strongest_session_key) {
145 const krb5_enctype *p;
146 krb5_enctype clientbest = (krb5_enctype)ETYPE_NULL;
147 int j;
150 * Pick the strongest key that the KDC, target service, and
151 * client all support, using the local cryptosystem enctype
152 * list in strongest-to-weakest order to drive the search.
154 * This is not what RFC4120 says to do, but it encourages
155 * adoption of stronger enctypes. This doesn't play well with
156 * clients that have multiple Kerberos client implementations
157 * available with different supported enctype lists.
160 /* drive the search with local supported enctypes list */
161 p = krb5_kerberos_enctypes(context);
162 for (i = 0;
163 p[i] != (krb5_enctype)ETYPE_NULL && enctype == (krb5_enctype)ETYPE_NULL;
164 i++) {
165 if (krb5_enctype_valid(context, p[i]) != 0)
166 continue;
168 /* check that the client supports it too */
169 for (j = 0; j < len && enctype == (krb5_enctype)ETYPE_NULL; j++) {
170 if (p[i] != etypes[j])
171 continue;
172 /* save best of union of { client, crypto system } */
173 if (clientbest == (krb5_enctype)ETYPE_NULL)
174 clientbest = p[i];
175 /* check target princ support */
176 ret = hdb_enctype2key(context, &princ->entry, NULL, p[i], &key);
177 if (ret)
178 continue;
179 if (is_preauth && !is_default_salt_p(&def_salt, key))
180 continue;
181 enctype = p[i];
184 if (clientbest != (krb5_enctype)ETYPE_NULL &&
185 enctype == (krb5_enctype)ETYPE_NULL)
186 enctype = clientbest;
187 else if (enctype == (krb5_enctype)ETYPE_NULL)
188 ret = KRB5KDC_ERR_ETYPE_NOSUPP;
189 if (ret == 0 && ret_enctype != NULL)
190 *ret_enctype = enctype;
191 if (ret == 0 && ret_key != NULL)
192 *ret_key = key;
193 } else {
195 * Pick the first key from the client's enctype list that is
196 * supported by the cryptosystem and by the given principal.
198 * RFC4120 says we SHOULD pick the first _strong_ key from the
199 * client's list... not the first key... If the admin disallows
200 * weak enctypes in krb5.conf and selects this key selection
201 * algorithm, then we get exactly what RFC4120 says.
203 for(key = NULL, i = 0; ret != 0 && i < len; i++, key = NULL) {
205 if (krb5_enctype_valid(context, etypes[i]) != 0 &&
206 !_kdc_is_weak_exception(princ->entry.principal, etypes[i]))
207 continue;
209 while (hdb_next_enctype2key(context, &princ->entry, NULL,
210 etypes[i], &key) == 0) {
211 if (key->key.keyvalue.length == 0) {
212 ret = KRB5KDC_ERR_NULL_KEY;
213 continue;
215 if (ret_key != NULL)
216 *ret_key = key;
217 if (ret_enctype != NULL)
218 *ret_enctype = etypes[i];
219 ret = 0;
220 if (is_preauth && is_default_salt_p(&def_salt, key))
221 goto out;
226 out:
227 krb5_free_salt (context, def_salt);
228 return ret;
231 krb5_error_code
232 _kdc_make_anonymous_principalname (PrincipalName *pn)
234 pn->name_type = KRB5_NT_PRINCIPAL;
235 pn->name_string.len = 1;
236 pn->name_string.val = malloc(sizeof(*pn->name_string.val));
237 if (pn->name_string.val == NULL)
238 return ENOMEM;
239 pn->name_string.val[0] = strdup("anonymous");
240 if (pn->name_string.val[0] == NULL) {
241 free(pn->name_string.val);
242 pn->name_string.val = NULL;
243 return ENOMEM;
245 return 0;
248 static void
249 _kdc_r_log(kdc_request_t r, int level, const char *fmt, ...)
251 va_list ap;
252 char *s;
253 va_start(ap, fmt);
254 s = kdc_log_msg_va(r->context, r->config, level, fmt, ap);
255 if(s) free(s);
256 va_end(ap);
259 static void
260 _kdc_set_e_text(kdc_request_t r, const char *e_text)
262 r->e_text = e_text;
263 kdc_log(r->context, r->config, 0, "%s", e_text);
266 void
267 _kdc_log_timestamp(krb5_context context,
268 krb5_kdc_configuration *config,
269 const char *type,
270 KerberosTime authtime, KerberosTime *starttime,
271 KerberosTime endtime, KerberosTime *renew_till)
273 char authtime_str[100], starttime_str[100],
274 endtime_str[100], renewtime_str[100];
276 krb5_format_time(context, authtime,
277 authtime_str, sizeof(authtime_str), TRUE);
278 if (starttime)
279 krb5_format_time(context, *starttime,
280 starttime_str, sizeof(starttime_str), TRUE);
281 else
282 strlcpy(starttime_str, "unset", sizeof(starttime_str));
283 krb5_format_time(context, endtime,
284 endtime_str, sizeof(endtime_str), TRUE);
285 if (renew_till)
286 krb5_format_time(context, *renew_till,
287 renewtime_str, sizeof(renewtime_str), TRUE);
288 else
289 strlcpy(renewtime_str, "unset", sizeof(renewtime_str));
291 kdc_log(context, config, 5,
292 "%s authtime: %s starttime: %s endtime: %s renew till: %s",
293 type, authtime_str, starttime_str, endtime_str, renewtime_str);
300 #ifdef PKINIT
302 static krb5_error_code
303 pa_pkinit_validate(kdc_request_t r, const PA_DATA *pa)
305 pk_client_params *pkp = NULL;
306 char *client_cert = NULL;
307 krb5_error_code ret;
309 ret = _kdc_pk_rd_padata(r->context, r->config, &r->req, pa, r->client, &pkp);
310 if (ret || pkp == NULL) {
311 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
312 _kdc_r_log(r, 5, "Failed to decode PKINIT PA-DATA -- %s",
313 r->client_name);
314 goto out;
317 ret = _kdc_pk_check_client(r->context,
318 r->config,
319 r->clientdb,
320 r->client,
321 pkp,
322 &client_cert);
323 if (ret) {
324 _kdc_set_e_text(r, "PKINIT certificate not allowed to "
325 "impersonate principal");
326 goto out;
329 _kdc_r_log(r, 0, "PKINIT pre-authentication succeeded -- %s using %s",
330 r->client_name, client_cert);
331 free(client_cert);
333 ret = _kdc_pk_mk_pa_reply(r->context, r->config, pkp, r->client,
334 r->sessionetype, &r->req, &r->request,
335 &r->reply_key, &r->session_key, &r->outpadata);
336 if (ret) {
337 _kdc_set_e_text(r, "Failed to build PK-INIT reply");
338 goto out;
340 #if 0
341 ret = _kdc_add_inital_verified_cas(r->context, r->config,
342 pkp, &r->et);
343 #endif
344 out:
345 if (pkp)
346 _kdc_pk_free_client_param(r->context, pkp);
348 return ret;
351 #endif /* PKINIT */
357 static krb5_error_code
358 make_pa_enc_challange(krb5_context context, METHOD_DATA *md,
359 krb5_crypto crypto)
361 PA_ENC_TS_ENC p;
362 unsigned char *buf;
363 size_t buf_size;
364 size_t len;
365 EncryptedData encdata;
366 krb5_error_code ret;
367 int32_t usec;
368 int usec2;
370 krb5_us_timeofday (context, &p.patimestamp, &usec);
371 usec2 = usec;
372 p.pausec = &usec2;
374 ASN1_MALLOC_ENCODE(PA_ENC_TS_ENC, buf, buf_size, &p, &len, ret);
375 if (ret)
376 return ret;
377 if(buf_size != len)
378 krb5_abortx(context, "internal error in ASN.1 encoder");
380 ret = krb5_encrypt_EncryptedData(context,
381 crypto,
382 KRB5_KU_ENC_CHALLENGE_KDC,
383 buf,
384 len,
386 &encdata);
387 free(buf);
388 if (ret)
389 return ret;
391 ASN1_MALLOC_ENCODE(EncryptedData, buf, buf_size, &encdata, &len, ret);
392 free_EncryptedData(&encdata);
393 if (ret)
394 return ret;
395 if(buf_size != len)
396 krb5_abortx(context, "internal error in ASN.1 encoder");
398 ret = krb5_padata_add(context, md, KRB5_PADATA_ENCRYPTED_CHALLENGE, buf, len);
399 if (ret)
400 free(buf);
401 return ret;
404 static krb5_error_code
405 pa_enc_chal_validate(kdc_request_t r, const PA_DATA *pa)
407 krb5_data pepper1, pepper2, ts_data;
408 KDC_REQ_BODY *b = &r->req.req_body;
409 EncryptedData enc_data;
410 krb5_enctype aenctype;
411 krb5_error_code ret;
412 struct Key *k;
413 size_t size;
414 int i;
416 heim_assert(r->armor_crypto != NULL, "ENC-CHAL called for non FAST");
418 if (b->kdc_options.request_anonymous) {
419 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
420 kdc_log(r->context, r->config, 0, "ENC-CHALL doesn't support anon");
421 return ret;
424 ret = decode_EncryptedData(pa->padata_value.data,
425 pa->padata_value.length,
426 &enc_data,
427 &size);
428 if (ret) {
429 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
430 _kdc_r_log(r, 5, "Failed to decode PA-DATA -- %s",
431 r->client_name);
432 return ret;
435 pepper1.data = "clientchallengearmor";
436 pepper1.length = strlen(pepper1.data);
437 pepper2.data = "challengelongterm";
438 pepper2.length = strlen(pepper2.data);
440 krb5_crypto_getenctype(r->context, r->armor_crypto, &aenctype);
442 for (i = 0; i < r->client->entry.keys.len; i++) {
443 krb5_crypto challangecrypto, longtermcrypto;
444 krb5_keyblock challangekey;
445 PA_ENC_TS_ENC p;
447 k = &r->client->entry.keys.val[i];
449 ret = krb5_crypto_init(r->context, &k->key, 0, &longtermcrypto);
450 if (ret)
451 continue;
453 ret = krb5_crypto_fx_cf2(r->context, r->armor_crypto, longtermcrypto,
454 &pepper1, &pepper2, aenctype,
455 &challangekey);
456 krb5_crypto_destroy(r->context, longtermcrypto);
457 if (ret)
458 continue;
460 ret = krb5_crypto_init(r->context, &challangekey, 0,
461 &challangecrypto);
462 if (ret)
463 continue;
465 ret = krb5_decrypt_EncryptedData(r->context, challangecrypto,
466 KRB5_KU_ENC_CHALLENGE_CLIENT,
467 &enc_data,
468 &ts_data);
469 if (ret)
470 continue;
472 ret = decode_PA_ENC_TS_ENC(ts_data.data,
473 ts_data.length,
475 &size);
476 krb5_data_free(&ts_data);
477 if(ret){
478 krb5_crypto_destroy(r->context, challangecrypto);
479 ret = KRB5KDC_ERR_PREAUTH_FAILED;
480 _kdc_r_log(r, 5, "Failed to decode PA-ENC-TS_ENC -- %s",
481 r->client_name);
482 continue;
485 if (abs(kdc_time - p.patimestamp) > r->context->max_skew) {
486 char client_time[100];
488 krb5_crypto_destroy(r->context, challangecrypto);
490 krb5_format_time(r->context, p.patimestamp,
491 client_time, sizeof(client_time), TRUE);
493 ret = KRB5KRB_AP_ERR_SKEW;
494 _kdc_r_log(r, 0, "Too large time skew, "
495 "client time %s is out by %u > %u seconds -- %s",
496 client_time,
497 (unsigned)abs(kdc_time - p.patimestamp),
498 r->context->max_skew,
499 r->client_name);
501 free_PA_ENC_TS_ENC(&p);
502 goto out;
505 free_PA_ENC_TS_ENC(&p);
507 ret = make_pa_enc_challange(r->context, &r->outpadata,
508 challangecrypto);
509 krb5_crypto_destroy(r->context, challangecrypto);
510 if (ret)
511 goto out;
513 set_salt_padata(&r->outpadata, k->salt);
514 krb5_free_keyblock_contents(r->context, &r->reply_key);
515 ret = krb5_copy_keyblock_contents(r->context, &k->key, &r->reply_key);
516 ret = 0;
517 break;
519 if (i < r->client->entry.keys.len)
520 ret = KRB5KDC_ERR_PREAUTH_FAILED;
521 out:
522 free_EncryptedData(&enc_data);
524 return ret;
527 static krb5_error_code
528 pa_enc_ts_validate(kdc_request_t r, const PA_DATA *pa)
530 EncryptedData enc_data;
531 krb5_error_code ret;
532 krb5_crypto crypto;
533 krb5_data ts_data;
534 PA_ENC_TS_ENC p;
535 size_t len;
536 Key *pa_key;
537 char *str;
539 if (r->req.req_body.kdc_options.request_anonymous) {
540 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
541 _kdc_set_e_text(r, "ENC-TS doesn't suport anon");
542 goto out;
545 ret = decode_EncryptedData(pa->padata_value.data,
546 pa->padata_value.length,
547 &enc_data,
548 &len);
549 if (ret) {
550 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
551 _kdc_r_log(r, 5, "Failed to decode PA-DATA -- %s",
552 r->client_name);
553 goto out;
556 ret = hdb_enctype2key(r->context, &r->client->entry, NULL,
557 enc_data.etype, &pa_key);
558 if(ret){
559 char *estr;
560 _kdc_set_e_text(r, "No key matching entype");
561 ret = KRB5KDC_ERR_ETYPE_NOSUPP;
562 if(krb5_enctype_to_string(r->context, enc_data.etype, &estr))
563 estr = NULL;
564 if(estr == NULL)
565 _kdc_r_log(r, 5,
566 "No client key matching pa-data (%d) -- %s",
567 enc_data.etype, r->client_name);
568 else
569 _kdc_r_log(r, 5,
570 "No client key matching pa-data (%s) -- %s",
571 estr, r->client_name);
572 free(estr);
573 free_EncryptedData(&enc_data);
574 goto out;
577 try_next_key:
578 ret = krb5_crypto_init(r->context, &pa_key->key, 0, &crypto);
579 if (ret) {
580 const char *msg = krb5_get_error_message(r->context, ret);
581 _kdc_r_log(r, 0, "krb5_crypto_init failed: %s", msg);
582 krb5_free_error_message(r->context, msg);
583 free_EncryptedData(&enc_data);
584 goto out;
587 ret = krb5_decrypt_EncryptedData (r->context,
588 crypto,
589 KRB5_KU_PA_ENC_TIMESTAMP,
590 &enc_data,
591 &ts_data);
592 krb5_crypto_destroy(r->context, crypto);
594 * Since the user might have several keys with the same
595 * enctype but with diffrent salting, we need to try all
596 * the keys with the same enctype.
598 if(ret){
599 krb5_error_code ret2;
600 const char *msg = krb5_get_error_message(r->context, ret);
602 ret2 = krb5_enctype_to_string(r->context,
603 pa_key->key.keytype, &str);
604 if (ret2)
605 str = NULL;
606 _kdc_r_log(r, 5, "Failed to decrypt PA-DATA -- %s "
607 "(enctype %s) error %s",
608 r->client_name, str ? str : "unknown enctype", msg);
609 krb5_free_error_message(r->context, msg);
610 free(str);
612 if(hdb_next_enctype2key(r->context, &r->client->entry, NULL,
613 enc_data.etype, &pa_key) == 0)
614 goto try_next_key;
616 free_EncryptedData(&enc_data);
618 if (r->clientdb->hdb_auth_status)
619 r->clientdb->hdb_auth_status(r->context, r->clientdb, r->client,
620 HDB_AUTH_WRONG_PASSWORD);
622 ret = KRB5KDC_ERR_PREAUTH_FAILED;
623 goto out;
625 free_EncryptedData(&enc_data);
626 ret = decode_PA_ENC_TS_ENC(ts_data.data,
627 ts_data.length,
629 &len);
630 krb5_data_free(&ts_data);
631 if(ret){
632 ret = KRB5KDC_ERR_PREAUTH_FAILED;
633 _kdc_r_log(r, 5, "Failed to decode PA-ENC-TS_ENC -- %s",
634 r->client_name);
635 goto out;
637 if (abs(kdc_time - p.patimestamp) > r->context->max_skew) {
638 char client_time[100];
640 krb5_format_time(r->context, p.patimestamp,
641 client_time, sizeof(client_time), TRUE);
643 ret = KRB5KRB_AP_ERR_SKEW;
644 _kdc_r_log(r, 0, "Too large time skew, "
645 "client time %s is out by %u > %u seconds -- %s",
646 client_time,
647 (unsigned)abs(kdc_time - p.patimestamp),
648 r->context->max_skew,
649 r->client_name);
652 * The following is needed to make windows clients to
653 * retry using the timestamp in the error message, if
654 * there is a e_text, they become unhappy.
656 r->e_text = NULL;
657 free_PA_ENC_TS_ENC(&p);
658 goto out;
660 free_PA_ENC_TS_ENC(&p);
662 set_salt_padata(&r->outpadata, pa_key->salt);
664 ret = krb5_copy_keyblock_contents(r->context, &pa_key->key, &r->reply_key);
665 if (ret)
666 return ret;
668 ret = krb5_enctype_to_string(r->context, pa_key->key.keytype, &str);
669 if (ret)
670 str = NULL;
671 _kdc_r_log(r, 2, "ENC-TS Pre-authentication succeeded -- %s using %s",
672 r->client_name, str ? str : "unknown enctype");
673 free(str);
675 ret = 0;
677 out:
679 return ret;
682 struct kdc_patypes {
683 int type;
684 char *name;
685 unsigned int flags;
686 #define PA_ANNOUNCE 1
687 #define PA_REQ_FAST 2 /* only use inside fast */
688 krb5_error_code (*validate)(kdc_request_t, const PA_DATA *pa);
691 static const struct kdc_patypes pat[] = {
692 #ifdef PKINIT
694 KRB5_PADATA_PK_AS_REQ, "PK-INIT(ietf)", PA_ANNOUNCE,
695 pa_pkinit_validate
698 KRB5_PADATA_PK_AS_REQ_WIN, "PK-INIT(win2k)", PA_ANNOUNCE,
699 pa_pkinit_validate
701 #else
702 { KRB5_PADATA_PK_AS_REQ, "PK-INIT(ietf)", 0, NULL },
703 { KRB5_PADATA_PK_AS_REQ_WIN, "PK-INIT(win2k)", 0, NULL },
704 #endif
705 { KRB5_PADATA_PA_PK_OCSP_RESPONSE , "OCSP", 0, NULL },
707 KRB5_PADATA_ENC_TIMESTAMP , "ENC-TS",
708 PA_ANNOUNCE,
709 pa_enc_ts_validate
712 KRB5_PADATA_ENCRYPTED_CHALLENGE , "ENC-CHAL",
713 PA_ANNOUNCE | PA_REQ_FAST,
714 pa_enc_chal_validate
716 { KRB5_PADATA_REQ_ENC_PA_REP , "REQ-ENC-PA-REP", 0, NULL },
717 { KRB5_PADATA_FX_FAST, "FX-FAST", PA_ANNOUNCE, NULL },
718 { KRB5_PADATA_FX_ERROR, "FX-ERROR", 0, NULL },
719 { KRB5_PADATA_FX_COOKIE, "FX-COOKIE", 0, NULL }
722 static void
723 log_patypes(krb5_context context,
724 krb5_kdc_configuration *config,
725 METHOD_DATA *padata)
727 struct rk_strpool *p = NULL;
728 char *str;
729 size_t n, m;
731 for (n = 0; n < padata->len; n++) {
732 for (m = 0; m < sizeof(pat) / sizeof(pat[0]); m++) {
733 if (padata->val[n].padata_type == pat[m].type) {
734 p = rk_strpoolprintf(p, "%s", pat[m].name);
735 break;
738 if (m == sizeof(pat) / sizeof(pat[0]))
739 p = rk_strpoolprintf(p, "%d", padata->val[n].padata_type);
740 if (p && n + 1 < padata->len)
741 p = rk_strpoolprintf(p, ", ");
742 if (p == NULL) {
743 kdc_log(context, config, 0, "out of memory");
744 return;
747 if (p == NULL)
748 p = rk_strpoolprintf(p, "none");
750 str = rk_strpoolcollect(p);
751 kdc_log(context, config, 0, "Client sent patypes: %s", str);
752 free(str);
759 krb5_error_code
760 _kdc_encode_reply(krb5_context context,
761 krb5_kdc_configuration *config,
762 krb5_crypto armor_crypto, uint32_t nonce,
763 KDC_REP *rep, EncTicketPart *et, EncKDCRepPart *ek,
764 krb5_enctype etype,
765 int skvno, const EncryptionKey *skey,
766 int ckvno, const EncryptionKey *reply_key,
767 int rk_is_subkey,
768 const char **e_text,
769 krb5_data *reply)
771 unsigned char *buf;
772 size_t buf_size;
773 size_t len = 0;
774 krb5_error_code ret;
775 krb5_crypto crypto;
777 ASN1_MALLOC_ENCODE(EncTicketPart, buf, buf_size, et, &len, ret);
778 if(ret) {
779 const char *msg = krb5_get_error_message(context, ret);
780 kdc_log(context, config, 0, "Failed to encode ticket: %s", msg);
781 krb5_free_error_message(context, msg);
782 return ret;
784 if(buf_size != len)
785 krb5_abortx(context, "Internal error in ASN.1 encoder");
787 ret = krb5_crypto_init(context, skey, etype, &crypto);
788 if (ret) {
789 const char *msg = krb5_get_error_message(context, ret);
790 kdc_log(context, config, 0, "krb5_crypto_init failed: %s", msg);
791 krb5_free_error_message(context, msg);
792 return ret;
795 ret = krb5_encrypt_EncryptedData(context,
796 crypto,
797 KRB5_KU_TICKET,
798 buf,
799 len,
800 skvno,
801 &rep->ticket.enc_part);
802 free(buf);
803 krb5_crypto_destroy(context, crypto);
804 if(ret) {
805 const char *msg = krb5_get_error_message(context, ret);
806 kdc_log(context, config, 0, "Failed to encrypt data: %s", msg);
807 krb5_free_error_message(context, msg);
808 return ret;
811 if (armor_crypto) {
812 krb5_data data;
813 krb5_keyblock *strengthen_key = NULL;
814 KrbFastFinished finished;
816 kdc_log(context, config, 0, "FAST armor protection");
818 memset(&finished, 0, sizeof(finished));
819 krb5_data_zero(&data);
821 finished.timestamp = kdc_time;
822 finished.usec = 0;
823 finished.crealm = et->crealm;
824 finished.cname = et->cname;
826 ASN1_MALLOC_ENCODE(Ticket, data.data, data.length,
827 &rep->ticket, &len, ret);
828 if (ret)
829 return ret;
830 if (data.length != len)
831 krb5_abortx(context, "internal asn.1 error");
833 ret = krb5_create_checksum(context, armor_crypto,
834 KRB5_KU_FAST_FINISHED, 0,
835 data.data, data.length,
836 &finished.ticket_checksum);
837 krb5_data_free(&data);
838 if (ret)
839 return ret;
841 ret = _kdc_fast_mk_response(context, armor_crypto,
842 rep->padata, strengthen_key, &finished,
843 nonce, &data);
844 free_Checksum(&finished.ticket_checksum);
845 if (ret)
846 return ret;
848 if (rep->padata) {
849 free_METHOD_DATA(rep->padata);
850 } else {
851 rep->padata = calloc(1, sizeof(*(rep->padata)));
852 if (rep->padata == NULL) {
853 krb5_data_free(&data);
854 return ENOMEM;
858 ret = krb5_padata_add(context, rep->padata,
859 KRB5_PADATA_FX_FAST,
860 data.data, data.length);
861 if (ret)
862 return ret;
865 * Hide client name of privacy reasons
867 if (1 /* r->fast_options.hide_client_names */) {
868 rep->crealm[0] = '\0';
869 free_PrincipalName(&rep->cname);
870 rep->cname.name_type = 0;
874 if(rep->msg_type == krb_as_rep && !config->encode_as_rep_as_tgs_rep)
875 ASN1_MALLOC_ENCODE(EncASRepPart, buf, buf_size, ek, &len, ret);
876 else
877 ASN1_MALLOC_ENCODE(EncTGSRepPart, buf, buf_size, ek, &len, ret);
878 if(ret) {
879 const char *msg = krb5_get_error_message(context, ret);
880 kdc_log(context, config, 0, "Failed to encode KDC-REP: %s", msg);
881 krb5_free_error_message(context, msg);
882 return ret;
884 if(buf_size != len) {
885 free(buf);
886 kdc_log(context, config, 0, "Internal error in ASN.1 encoder");
887 *e_text = "KDC internal error";
888 return KRB5KRB_ERR_GENERIC;
890 ret = krb5_crypto_init(context, reply_key, 0, &crypto);
891 if (ret) {
892 const char *msg = krb5_get_error_message(context, ret);
893 free(buf);
894 kdc_log(context, config, 0, "krb5_crypto_init failed: %s", msg);
895 krb5_free_error_message(context, msg);
896 return ret;
898 if(rep->msg_type == krb_as_rep) {
899 krb5_encrypt_EncryptedData(context,
900 crypto,
901 KRB5_KU_AS_REP_ENC_PART,
902 buf,
903 len,
904 ckvno,
905 &rep->enc_part);
906 free(buf);
907 ASN1_MALLOC_ENCODE(AS_REP, buf, buf_size, rep, &len, ret);
908 } else {
909 krb5_encrypt_EncryptedData(context,
910 crypto,
911 rk_is_subkey ? KRB5_KU_TGS_REP_ENC_PART_SUB_KEY : KRB5_KU_TGS_REP_ENC_PART_SESSION,
912 buf,
913 len,
914 ckvno,
915 &rep->enc_part);
916 free(buf);
917 ASN1_MALLOC_ENCODE(TGS_REP, buf, buf_size, rep, &len, ret);
919 krb5_crypto_destroy(context, crypto);
920 if(ret) {
921 const char *msg = krb5_get_error_message(context, ret);
922 kdc_log(context, config, 0, "Failed to encode KDC-REP: %s", msg);
923 krb5_free_error_message(context, msg);
924 return ret;
926 if(buf_size != len) {
927 free(buf);
928 kdc_log(context, config, 0, "Internal error in ASN.1 encoder");
929 *e_text = "KDC internal error";
930 return KRB5KRB_ERR_GENERIC;
932 reply->data = buf;
933 reply->length = buf_size;
934 return 0;
938 * Return 1 if the client have only older enctypes, this is for
939 * determining if the server should send ETYPE_INFO2 or not.
942 static int
943 older_enctype(krb5_enctype enctype)
945 switch (enctype) {
946 case ETYPE_DES_CBC_CRC:
947 case ETYPE_DES_CBC_MD4:
948 case ETYPE_DES_CBC_MD5:
949 case ETYPE_DES3_CBC_SHA1:
950 case ETYPE_ARCFOUR_HMAC_MD5:
951 case ETYPE_ARCFOUR_HMAC_MD5_56:
953 * The following three is "old" windows enctypes and is needed for
954 * windows 2000 hosts.
956 case ETYPE_ARCFOUR_MD4:
957 case ETYPE_ARCFOUR_HMAC_OLD:
958 case ETYPE_ARCFOUR_HMAC_OLD_EXP:
959 return 1;
960 default:
961 return 0;
969 static krb5_error_code
970 make_etype_info_entry(krb5_context context, ETYPE_INFO_ENTRY *ent, Key *key)
972 ent->etype = key->key.keytype;
973 if(key->salt){
974 #if 0
975 ALLOC(ent->salttype);
977 if(key->salt->type == hdb_pw_salt)
978 *ent->salttype = 0; /* or 1? or NULL? */
979 else if(key->salt->type == hdb_afs3_salt)
980 *ent->salttype = 2;
981 else {
982 kdc_log(context, config, 0, "unknown salt-type: %d",
983 key->salt->type);
984 return KRB5KRB_ERR_GENERIC;
986 /* according to `the specs', we can't send a salt if
987 we have AFS3 salted key, but that requires that you
988 *know* what cell you are using (e.g by assuming
989 that the cell is the same as the realm in lower
990 case) */
991 #elif 0
992 ALLOC(ent->salttype);
993 *ent->salttype = key->salt->type;
994 #else
996 * We shouldn't sent salttype since it is incompatible with the
997 * specification and it breaks windows clients. The afs
998 * salting problem is solved by using KRB5-PADATA-AFS3-SALT
999 * implemented in Heimdal 0.7 and later.
1001 ent->salttype = NULL;
1002 #endif
1003 krb5_copy_data(context, &key->salt->salt,
1004 &ent->salt);
1005 } else {
1006 /* we return no salt type at all, as that should indicate
1007 * the default salt type and make everybody happy. some
1008 * systems (like w2k) dislike being told the salt type
1009 * here. */
1011 ent->salttype = NULL;
1012 ent->salt = NULL;
1014 return 0;
1017 static krb5_error_code
1018 get_pa_etype_info(krb5_context context,
1019 krb5_kdc_configuration *config,
1020 METHOD_DATA *md, Key *ckey)
1022 krb5_error_code ret = 0;
1023 ETYPE_INFO pa;
1024 unsigned char *buf;
1025 size_t len;
1028 pa.len = 1;
1029 pa.val = calloc(1, sizeof(pa.val[0]));
1030 if(pa.val == NULL)
1031 return ENOMEM;
1033 ret = make_etype_info_entry(context, &pa.val[0], ckey);
1034 if (ret) {
1035 free_ETYPE_INFO(&pa);
1036 return ret;
1039 ASN1_MALLOC_ENCODE(ETYPE_INFO, buf, len, &pa, &len, ret);
1040 free_ETYPE_INFO(&pa);
1041 if(ret)
1042 return ret;
1043 ret = realloc_method_data(md);
1044 if(ret) {
1045 free(buf);
1046 return ret;
1048 md->val[md->len - 1].padata_type = KRB5_PADATA_ETYPE_INFO;
1049 md->val[md->len - 1].padata_value.length = len;
1050 md->val[md->len - 1].padata_value.data = buf;
1051 return 0;
1058 extern int _krb5_AES_string_to_default_iterator;
1060 static krb5_error_code
1061 make_etype_info2_entry(ETYPE_INFO2_ENTRY *ent, Key *key)
1063 ent->etype = key->key.keytype;
1064 if(key->salt) {
1065 ALLOC(ent->salt);
1066 if (ent->salt == NULL)
1067 return ENOMEM;
1068 *ent->salt = malloc(key->salt->salt.length + 1);
1069 if (*ent->salt == NULL) {
1070 free(ent->salt);
1071 ent->salt = NULL;
1072 return ENOMEM;
1074 memcpy(*ent->salt, key->salt->salt.data, key->salt->salt.length);
1075 (*ent->salt)[key->salt->salt.length] = '\0';
1076 } else
1077 ent->salt = NULL;
1079 ent->s2kparams = NULL;
1081 switch (key->key.keytype) {
1082 case ETYPE_AES128_CTS_HMAC_SHA1_96:
1083 case ETYPE_AES256_CTS_HMAC_SHA1_96:
1084 ALLOC(ent->s2kparams);
1085 if (ent->s2kparams == NULL)
1086 return ENOMEM;
1087 ent->s2kparams->length = 4;
1088 ent->s2kparams->data = malloc(ent->s2kparams->length);
1089 if (ent->s2kparams->data == NULL) {
1090 free(ent->s2kparams);
1091 ent->s2kparams = NULL;
1092 return ENOMEM;
1094 _krb5_put_int(ent->s2kparams->data,
1095 _krb5_AES_string_to_default_iterator,
1096 ent->s2kparams->length);
1097 break;
1098 case ETYPE_DES_CBC_CRC:
1099 case ETYPE_DES_CBC_MD4:
1100 case ETYPE_DES_CBC_MD5:
1101 /* Check if this was a AFS3 salted key */
1102 if(key->salt && key->salt->type == hdb_afs3_salt){
1103 ALLOC(ent->s2kparams);
1104 if (ent->s2kparams == NULL)
1105 return ENOMEM;
1106 ent->s2kparams->length = 1;
1107 ent->s2kparams->data = malloc(ent->s2kparams->length);
1108 if (ent->s2kparams->data == NULL) {
1109 free(ent->s2kparams);
1110 ent->s2kparams = NULL;
1111 return ENOMEM;
1113 _krb5_put_int(ent->s2kparams->data,
1115 ent->s2kparams->length);
1117 break;
1118 default:
1119 break;
1121 return 0;
1125 * Return an ETYPE-INFO2. Enctypes are storted the same way as in the
1126 * database (client supported enctypes first, then the unsupported
1127 * enctypes).
1130 static krb5_error_code
1131 get_pa_etype_info2(krb5_context context,
1132 krb5_kdc_configuration *config,
1133 METHOD_DATA *md, Key *ckey)
1135 krb5_error_code ret = 0;
1136 ETYPE_INFO2 pa;
1137 unsigned char *buf;
1138 size_t len;
1140 pa.len = 1;
1141 pa.val = calloc(1, sizeof(pa.val[0]));
1142 if(pa.val == NULL)
1143 return ENOMEM;
1145 ret = make_etype_info2_entry(&pa.val[0], ckey);
1146 if (ret) {
1147 free_ETYPE_INFO2(&pa);
1148 return ret;
1151 ASN1_MALLOC_ENCODE(ETYPE_INFO2, buf, len, &pa, &len, ret);
1152 free_ETYPE_INFO2(&pa);
1153 if(ret)
1154 return ret;
1155 ret = realloc_method_data(md);
1156 if(ret) {
1157 free(buf);
1158 return ret;
1160 md->val[md->len - 1].padata_type = KRB5_PADATA_ETYPE_INFO2;
1161 md->val[md->len - 1].padata_value.length = len;
1162 md->val[md->len - 1].padata_value.data = buf;
1163 return 0;
1170 static void
1171 log_as_req(krb5_context context,
1172 krb5_kdc_configuration *config,
1173 krb5_enctype cetype,
1174 krb5_enctype setype,
1175 const KDC_REQ_BODY *b)
1177 krb5_error_code ret;
1178 struct rk_strpool *p;
1179 char *str;
1180 size_t i;
1182 p = rk_strpoolprintf(NULL, "%s", "Client supported enctypes: ");
1184 for (i = 0; i < b->etype.len; i++) {
1185 ret = krb5_enctype_to_string(context, b->etype.val[i], &str);
1186 if (ret == 0) {
1187 p = rk_strpoolprintf(p, "%s", str);
1188 free(str);
1189 } else
1190 p = rk_strpoolprintf(p, "%d", b->etype.val[i]);
1191 if (p && i + 1 < b->etype.len)
1192 p = rk_strpoolprintf(p, ", ");
1193 if (p == NULL) {
1194 kdc_log(context, config, 0, "out of memory");
1195 return;
1198 if (p == NULL)
1199 p = rk_strpoolprintf(p, "no encryption types");
1202 char *cet;
1203 char *set;
1205 ret = krb5_enctype_to_string(context, cetype, &cet);
1206 if(ret == 0) {
1207 ret = krb5_enctype_to_string(context, setype, &set);
1208 if (ret == 0) {
1209 p = rk_strpoolprintf(p, ", using %s/%s", cet, set);
1210 free(set);
1212 free(cet);
1214 if (ret != 0)
1215 p = rk_strpoolprintf(p, ", using enctypes %d/%d",
1216 cetype, setype);
1219 str = rk_strpoolcollect(p);
1220 kdc_log(context, config, 0, "%s", str);
1221 free(str);
1224 char fixedstr[128];
1225 unparse_flags(KDCOptions2int(b->kdc_options), asn1_KDCOptions_units(),
1226 fixedstr, sizeof(fixedstr));
1227 if(*fixedstr)
1228 kdc_log(context, config, 0, "Requested flags: %s", fixedstr);
1233 * verify the flags on `client' and `server', returning 0
1234 * if they are OK and generating an error messages and returning
1235 * and error code otherwise.
1238 krb5_error_code
1239 kdc_check_flags(krb5_context context,
1240 krb5_kdc_configuration *config,
1241 hdb_entry_ex *client_ex, const char *client_name,
1242 hdb_entry_ex *server_ex, const char *server_name,
1243 krb5_boolean is_as_req)
1245 if(client_ex != NULL) {
1246 hdb_entry *client = &client_ex->entry;
1248 /* check client */
1249 if (client->flags.locked_out) {
1250 kdc_log(context, config, 0,
1251 "Client (%s) is locked out", client_name);
1252 return KRB5KDC_ERR_POLICY;
1255 if (client->flags.invalid) {
1256 kdc_log(context, config, 0,
1257 "Client (%s) has invalid bit set", client_name);
1258 return KRB5KDC_ERR_POLICY;
1261 if(!client->flags.client){
1262 kdc_log(context, config, 0,
1263 "Principal may not act as client -- %s", client_name);
1264 return KRB5KDC_ERR_POLICY;
1267 if (client->valid_start && *client->valid_start > kdc_time) {
1268 char starttime_str[100];
1269 krb5_format_time(context, *client->valid_start,
1270 starttime_str, sizeof(starttime_str), TRUE);
1271 kdc_log(context, config, 0,
1272 "Client not yet valid until %s -- %s",
1273 starttime_str, client_name);
1274 return KRB5KDC_ERR_CLIENT_NOTYET;
1277 if (client->valid_end && *client->valid_end < kdc_time) {
1278 char endtime_str[100];
1279 krb5_format_time(context, *client->valid_end,
1280 endtime_str, sizeof(endtime_str), TRUE);
1281 kdc_log(context, config, 0,
1282 "Client expired at %s -- %s",
1283 endtime_str, client_name);
1284 return KRB5KDC_ERR_NAME_EXP;
1287 if (client->flags.require_pwchange &&
1288 (server_ex == NULL || !server_ex->entry.flags.change_pw)) {
1289 kdc_log(context, config, 0,
1290 "Client's key must be changed -- %s", client_name);
1291 return KRB5KDC_ERR_KEY_EXPIRED;
1294 if (client->pw_end && *client->pw_end < kdc_time
1295 && (server_ex == NULL || !server_ex->entry.flags.change_pw)) {
1296 char pwend_str[100];
1297 krb5_format_time(context, *client->pw_end,
1298 pwend_str, sizeof(pwend_str), TRUE);
1299 kdc_log(context, config, 0,
1300 "Client's key has expired at %s -- %s",
1301 pwend_str, client_name);
1302 return KRB5KDC_ERR_KEY_EXPIRED;
1306 /* check server */
1308 if (server_ex != NULL) {
1309 hdb_entry *server = &server_ex->entry;
1311 if (server->flags.locked_out) {
1312 kdc_log(context, config, 0,
1313 "Client server locked out -- %s", server_name);
1314 return KRB5KDC_ERR_POLICY;
1316 if (server->flags.invalid) {
1317 kdc_log(context, config, 0,
1318 "Server has invalid flag set -- %s", server_name);
1319 return KRB5KDC_ERR_POLICY;
1322 if(!server->flags.server){
1323 kdc_log(context, config, 0,
1324 "Principal may not act as server -- %s", server_name);
1325 return KRB5KDC_ERR_POLICY;
1328 if(!is_as_req && server->flags.initial) {
1329 kdc_log(context, config, 0,
1330 "AS-REQ is required for server -- %s", server_name);
1331 return KRB5KDC_ERR_POLICY;
1334 if (server->valid_start && *server->valid_start > kdc_time) {
1335 char starttime_str[100];
1336 krb5_format_time(context, *server->valid_start,
1337 starttime_str, sizeof(starttime_str), TRUE);
1338 kdc_log(context, config, 0,
1339 "Server not yet valid until %s -- %s",
1340 starttime_str, server_name);
1341 return KRB5KDC_ERR_SERVICE_NOTYET;
1344 if (server->valid_end && *server->valid_end < kdc_time) {
1345 char endtime_str[100];
1346 krb5_format_time(context, *server->valid_end,
1347 endtime_str, sizeof(endtime_str), TRUE);
1348 kdc_log(context, config, 0,
1349 "Server expired at %s -- %s",
1350 endtime_str, server_name);
1351 return KRB5KDC_ERR_SERVICE_EXP;
1354 if (server->pw_end && *server->pw_end < kdc_time) {
1355 char pwend_str[100];
1356 krb5_format_time(context, *server->pw_end,
1357 pwend_str, sizeof(pwend_str), TRUE);
1358 kdc_log(context, config, 0,
1359 "Server's key has expired at -- %s",
1360 pwend_str, server_name);
1361 return KRB5KDC_ERR_KEY_EXPIRED;
1364 return 0;
1368 * Return TRUE if `from' is part of `addresses' taking into consideration
1369 * the configuration variables that tells us how strict we should be about
1370 * these checks
1373 krb5_boolean
1374 _kdc_check_addresses(krb5_context context,
1375 krb5_kdc_configuration *config,
1376 HostAddresses *addresses, const struct sockaddr *from)
1378 krb5_error_code ret;
1379 krb5_address addr;
1380 krb5_boolean result;
1381 krb5_boolean only_netbios = TRUE;
1382 size_t i;
1384 if(config->check_ticket_addresses == 0)
1385 return TRUE;
1387 if(addresses == NULL)
1388 return config->allow_null_ticket_addresses;
1390 for (i = 0; i < addresses->len; ++i) {
1391 if (addresses->val[i].addr_type != KRB5_ADDRESS_NETBIOS) {
1392 only_netbios = FALSE;
1396 /* Windows sends it's netbios name, which I can only assume is
1397 * used for the 'allowed workstations' check. This is painful,
1398 * but we still want to check IP addresses if they happen to be
1399 * present.
1402 if(only_netbios)
1403 return config->allow_null_ticket_addresses;
1405 ret = krb5_sockaddr2address (context, from, &addr);
1406 if(ret)
1407 return FALSE;
1409 result = krb5_address_search(context, &addr, addresses);
1410 krb5_free_address (context, &addr);
1411 return result;
1418 static krb5_boolean
1419 send_pac_p(krb5_context context, KDC_REQ *req)
1421 krb5_error_code ret;
1422 PA_PAC_REQUEST pacreq;
1423 const PA_DATA *pa;
1424 int i = 0;
1426 pa = _kdc_find_padata(req, &i, KRB5_PADATA_PA_PAC_REQUEST);
1427 if (pa == NULL)
1428 return TRUE;
1430 ret = decode_PA_PAC_REQUEST(pa->padata_value.data,
1431 pa->padata_value.length,
1432 &pacreq,
1433 NULL);
1434 if (ret)
1435 return TRUE;
1436 i = pacreq.include_pac;
1437 free_PA_PAC_REQUEST(&pacreq);
1438 if (i == 0)
1439 return FALSE;
1440 return TRUE;
1447 static krb5_error_code
1448 generate_pac(kdc_request_t r, Key *skey)
1450 krb5_error_code ret;
1451 krb5_pac p = NULL;
1452 krb5_data data;
1454 ret = _kdc_pac_generate(r->context, r->client, &p);
1455 if (ret) {
1456 _kdc_r_log(r, 0, "PAC generation failed for -- %s",
1457 r->client_name);
1458 return ret;
1460 if (p == NULL)
1461 return 0;
1463 ret = _krb5_pac_sign(r->context, p, r->et.authtime,
1464 r->client->entry.principal,
1465 &skey->key, /* Server key */
1466 &skey->key, /* FIXME: should be krbtgt key */
1467 &data);
1468 krb5_pac_free(r->context, p);
1469 if (ret) {
1470 _kdc_r_log(r, 0, "PAC signing failed for -- %s",
1471 r->client_name);
1472 return ret;
1475 ret = _kdc_tkt_add_if_relevant_ad(r->context, &r->et,
1476 KRB5_AUTHDATA_WIN2K_PAC,
1477 &data);
1478 krb5_data_free(&data);
1480 return ret;
1487 krb5_boolean
1488 _kdc_is_anonymous(krb5_context context, krb5_principal principal)
1490 if (principal->name.name_type != KRB5_NT_WELLKNOWN ||
1491 principal->name.name_string.len != 2 ||
1492 strcmp(principal->name.name_string.val[0], KRB5_WELLKNOWN_NAME) != 0 ||
1493 strcmp(principal->name.name_string.val[1], KRB5_ANON_NAME) != 0)
1494 return 0;
1495 return 1;
1498 static int
1499 require_preauth_p(kdc_request_t r)
1501 return r->config->require_preauth
1502 || r->client->entry.flags.require_preauth
1503 || r->server->entry.flags.require_preauth;
1511 static krb5_error_code
1512 add_enc_pa_rep(kdc_request_t r)
1514 krb5_error_code ret;
1515 krb5_crypto crypto;
1516 Checksum checksum;
1517 krb5_data cdata;
1518 size_t len;
1520 r->et.flags.enc_pa_rep = r->ek.flags.enc_pa_rep = 1;
1522 ret = krb5_crypto_init(r->context, &r->reply_key, 0, &crypto);
1523 if (ret)
1524 return ret;
1526 ret = krb5_create_checksum(r->context, crypto,
1527 KRB5_KU_AS_REQ, 0,
1528 r->request.data, r->request.length,
1529 &checksum);
1530 krb5_crypto_destroy(r->context, crypto);
1531 if (ret)
1532 return ret;
1534 ASN1_MALLOC_ENCODE(Checksum, cdata.data, cdata.length,
1535 &checksum, &len, ret);
1536 free_Checksum(&checksum);
1537 if (ret)
1538 return ret;
1539 heim_assert(cdata.length == len, "ASN.1 internal error");
1541 if (r->ek.encrypted_pa_data == NULL) {
1542 ALLOC(r->ek.encrypted_pa_data);
1543 if (r->ek.encrypted_pa_data == NULL)
1544 return ENOMEM;
1546 ret = krb5_padata_add(r->context, r->ek.encrypted_pa_data,
1547 KRB5_PADATA_REQ_ENC_PA_REP, cdata.data, cdata.length);
1548 if (ret)
1549 return ret;
1551 return krb5_padata_add(r->context, r->ek.encrypted_pa_data,
1552 KRB5_PADATA_FX_FAST, NULL, 0);
1559 krb5_error_code
1560 _kdc_as_rep(kdc_request_t r,
1561 krb5_data *reply,
1562 const char *from,
1563 struct sockaddr *from_addr,
1564 int datagram_reply)
1566 krb5_context context = r->context;
1567 krb5_kdc_configuration *config = r->config;
1568 KDC_REQ *req = &r->req;
1569 KDC_REQ_BODY *b = NULL;
1570 AS_REP rep;
1571 KDCOptions f;
1572 krb5_enctype setype;
1573 krb5_error_code ret = 0;
1574 Key *ckey, *skey;
1575 int found_pa = 0;
1576 int i, flags = HDB_F_FOR_AS_REQ;
1577 METHOD_DATA error_method;
1578 const PA_DATA *pa;
1580 memset(&rep, 0, sizeof(rep));
1581 error_method.len = 0;
1582 error_method.val = NULL;
1585 * Look for FAST armor and unwrap
1587 ret = _kdc_fast_unwrap_request(r);
1588 if (ret) {
1589 _kdc_r_log(r, 0, "FAST unwrap request from %s failed: %d", from, ret);
1590 goto out;
1593 b = &req->req_body;
1594 f = b->kdc_options;
1596 if (f.canonicalize)
1597 flags |= HDB_F_CANON;
1599 if(b->sname == NULL){
1600 ret = KRB5KRB_ERR_GENERIC;
1601 _kdc_set_e_text(r, "No server in request");
1602 } else{
1603 ret = _krb5_principalname2krb5_principal (context,
1604 &r->server_princ,
1605 *(b->sname),
1606 b->realm);
1607 if (ret == 0)
1608 ret = krb5_unparse_name(context, r->server_princ, &r->server_name);
1610 if (ret) {
1611 kdc_log(context, config, 0,
1612 "AS-REQ malformed server name from %s", from);
1613 goto out;
1615 if(b->cname == NULL){
1616 ret = KRB5KRB_ERR_GENERIC;
1617 _kdc_set_e_text(r, "No client in request");
1618 } else {
1619 ret = _krb5_principalname2krb5_principal (context,
1620 &r->client_princ,
1621 *(b->cname),
1622 b->realm);
1623 if (ret)
1624 goto out;
1626 ret = krb5_unparse_name(context, r->client_princ, &r->client_name);
1628 if (ret) {
1629 kdc_log(context, config, 0,
1630 "AS-REQ malformed client name from %s", from);
1631 goto out;
1634 kdc_log(context, config, 0, "AS-REQ %s from %s for %s",
1635 r->client_name, from, r->server_name);
1641 if (_kdc_is_anonymous(context, r->client_princ)) {
1642 if (!b->kdc_options.request_anonymous) {
1643 kdc_log(context, config, 0, "Anonymous ticket w/o anonymous flag");
1644 ret = KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN;
1645 goto out;
1647 } else if (b->kdc_options.request_anonymous) {
1648 kdc_log(context, config, 0,
1649 "Request for a anonymous ticket with non "
1650 "anonymous client name: %s", r->client_name);
1651 ret = KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN;
1652 goto out;
1659 ret = _kdc_db_fetch(context, config, r->client_princ,
1660 HDB_F_GET_CLIENT | flags, NULL,
1661 &r->clientdb, &r->client);
1662 if(ret == HDB_ERR_NOT_FOUND_HERE) {
1663 kdc_log(context, config, 5, "client %s does not have secrets at this KDC, need to proxy",
1664 r->client_name);
1665 goto out;
1666 } else if(ret){
1667 const char *msg = krb5_get_error_message(context, ret);
1668 kdc_log(context, config, 0, "UNKNOWN -- %s: %s", r->client_name, msg);
1669 krb5_free_error_message(context, msg);
1670 ret = KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN;
1671 goto out;
1673 ret = _kdc_db_fetch(context, config, r->server_princ,
1674 HDB_F_GET_SERVER|HDB_F_GET_KRBTGT | flags,
1675 NULL, NULL, &r->server);
1676 if(ret == HDB_ERR_NOT_FOUND_HERE) {
1677 kdc_log(context, config, 5, "target %s does not have secrets at this KDC, need to proxy",
1678 r->server_name);
1679 goto out;
1680 } else if(ret){
1681 const char *msg = krb5_get_error_message(context, ret);
1682 kdc_log(context, config, 0, "UNKNOWN -- %s: %s", r->server_name, msg);
1683 krb5_free_error_message(context, msg);
1684 ret = KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN;
1685 goto out;
1689 * Select a session enctype from the list of the crypto system
1690 * supported enctypes that is supported by the client and is one of
1691 * the enctype of the enctype of the service (likely krbtgt).
1693 * The latter is used as a hint of what enctypes all KDC support,
1694 * to make sure a newer version of KDC won't generate a session
1695 * enctype that an older version of a KDC in the same realm can't
1696 * decrypt.
1699 ret = _kdc_find_etype(context,
1700 krb5_principal_is_krbtgt(context, r->server_princ) ?
1701 config->tgt_use_strongest_session_key :
1702 config->svc_use_strongest_session_key, FALSE,
1703 r->client, b->etype.val, b->etype.len, &r->sessionetype,
1704 NULL);
1705 if (ret) {
1706 kdc_log(context, config, 0,
1707 "Client (%s) from %s has no common enctypes with KDC "
1708 "to use for the session key",
1709 r->client_name, from);
1710 goto out;
1714 * Pre-auth processing
1717 if(req->padata){
1718 unsigned int n;
1720 log_patypes(context, config, req->padata);
1722 /* Check if preauth matching */
1724 for (n = 0; !found_pa && n < sizeof(pat) / sizeof(pat[0]); n++) {
1725 if (pat[n].validate == NULL)
1726 continue;
1727 if (r->armor_crypto == NULL && (pat[n].flags & PA_REQ_FAST))
1728 continue;
1730 kdc_log(context, config, 5,
1731 "Looking for %s pa-data -- %s", pat[n].name, r->client_name);
1732 i = 0;
1733 pa = _kdc_find_padata(req, &i, pat[n].type);
1734 if (pa) {
1735 ret = pat[n].validate(r, pa);
1736 if (ret == 0) {
1737 kdc_log(context, config, 0,
1738 "%s pre-authentication succeeded -- %s",
1739 pat[n].name, r->client_name);
1740 found_pa = 1;
1741 r->et.flags.pre_authent = 1;
1747 if (found_pa == 0 && (require_preauth_p(r) || b->kdc_options.request_anonymous)) {
1748 size_t n;
1750 for (n = 0; n < sizeof(pat) / sizeof(pat[0]); n++) {
1751 if ((pat[n].flags & PA_ANNOUNCE) == 0)
1752 continue;
1753 ret = krb5_padata_add(context, &error_method,
1754 pat[n].type, NULL, 0);
1755 if (ret)
1756 goto out;
1760 * If there is a client key, send ETYPE_INFO{,2}
1762 ret = _kdc_find_etype(context,
1763 config->preauth_use_strongest_session_key, TRUE,
1764 r->client, b->etype.val, b->etype.len, NULL, &ckey);
1765 if (ret == 0) {
1768 * RFC4120 requires:
1769 * - If the client only knows about old enctypes, then send
1770 * both info replies (we send 'info' first in the list).
1771 * - If the client is 'modern', because it knows about 'new'
1772 * enctype types, then only send the 'info2' reply.
1774 * Before we send the full list of etype-info data, we pick
1775 * the client key we would have used anyway below, just pick
1776 * that instead.
1779 if (older_enctype(ckey->key.keytype)) {
1780 ret = get_pa_etype_info(context, config,
1781 &error_method, ckey);
1782 if (ret)
1783 goto out;
1785 ret = get_pa_etype_info2(context, config,
1786 &error_method, ckey);
1787 if (ret)
1788 goto out;
1791 ret = KRB5KDC_ERR_PREAUTH_REQUIRED;
1792 _kdc_set_e_text(r, "Need to use PA-ENC-TIMESTAMP/PA-PK-AS-REQ");
1793 goto out;
1796 if (r->clientdb->hdb_auth_status)
1797 r->clientdb->hdb_auth_status(context, r->clientdb, r->client,
1798 HDB_AUTH_SUCCESS);
1801 * Verify flags after the user been required to prove its identity
1802 * with in a preauth mech.
1805 ret = _kdc_check_access(context, config, r->client, r->client_name,
1806 r->server, r->server_name,
1807 req, &error_method);
1808 if(ret)
1809 goto out;
1812 * Select the best encryption type for the KDC with out regard to
1813 * the client since the client never needs to read that data.
1816 ret = _kdc_get_preferred_key(context, config,
1817 r->server, r->server_name,
1818 &setype, &skey);
1819 if(ret)
1820 goto out;
1822 if(f.renew || f.validate || f.proxy || f.forwarded || f.enc_tkt_in_skey
1823 || (f.request_anonymous && !config->allow_anonymous)) {
1824 ret = KRB5KDC_ERR_BADOPTION;
1825 _kdc_set_e_text(r, "Bad KDC options");
1826 goto out;
1830 * Build reply
1833 rep.pvno = 5;
1834 rep.msg_type = krb_as_rep;
1836 ret = copy_Realm(&r->client->entry.principal->realm, &rep.crealm);
1837 if (ret)
1838 goto out;
1839 ret = _krb5_principal2principalname(&rep.cname, r->client->entry.principal);
1840 if (ret)
1841 goto out;
1843 rep.ticket.tkt_vno = 5;
1844 copy_Realm(&r->server->entry.principal->realm, &rep.ticket.realm);
1845 _krb5_principal2principalname(&rep.ticket.sname,
1846 r->server->entry.principal);
1847 /* java 1.6 expects the name to be the same type, lets allow that
1848 * uncomplicated name-types. */
1849 #define CNT(sp,t) (((sp)->sname->name_type) == KRB5_NT_##t)
1850 if (CNT(b, UNKNOWN) || CNT(b, PRINCIPAL) || CNT(b, SRV_INST) || CNT(b, SRV_HST) || CNT(b, SRV_XHST))
1851 rep.ticket.sname.name_type = b->sname->name_type;
1852 #undef CNT
1854 r->et.flags.initial = 1;
1855 if(r->client->entry.flags.forwardable && r->server->entry.flags.forwardable)
1856 r->et.flags.forwardable = f.forwardable;
1857 else if (f.forwardable) {
1858 _kdc_set_e_text(r, "Ticket may not be forwardable");
1859 ret = KRB5KDC_ERR_POLICY;
1860 goto out;
1862 if(r->client->entry.flags.proxiable && r->server->entry.flags.proxiable)
1863 r->et.flags.proxiable = f.proxiable;
1864 else if (f.proxiable) {
1865 _kdc_set_e_text(r, "Ticket may not be proxiable");
1866 ret = KRB5KDC_ERR_POLICY;
1867 goto out;
1869 if(r->client->entry.flags.postdate && r->server->entry.flags.postdate)
1870 r->et.flags.may_postdate = f.allow_postdate;
1871 else if (f.allow_postdate){
1872 _kdc_set_e_text(r, "Ticket may not be postdate");
1873 ret = KRB5KDC_ERR_POLICY;
1874 goto out;
1877 /* check for valid set of addresses */
1878 if(!_kdc_check_addresses(context, config, b->addresses, from_addr)) {
1879 _kdc_set_e_text(r, "Bad address list in requested");
1880 ret = KRB5KRB_AP_ERR_BADADDR;
1881 goto out;
1884 ret = copy_PrincipalName(&rep.cname, &r->et.cname);
1885 if (ret)
1886 goto out;
1887 ret = copy_Realm(&rep.crealm, &r->et.crealm);
1888 if (ret)
1889 goto out;
1892 time_t start;
1893 time_t t;
1895 start = r->et.authtime = kdc_time;
1897 if(f.postdated && req->req_body.from){
1898 ALLOC(r->et.starttime);
1899 start = *r->et.starttime = *req->req_body.from;
1900 r->et.flags.invalid = 1;
1901 r->et.flags.postdated = 1; /* XXX ??? */
1903 _kdc_fix_time(&b->till);
1904 t = *b->till;
1906 /* be careful not overflowing */
1908 if(r->client->entry.max_life)
1909 t = start + min(t - start, *r->client->entry.max_life);
1910 if(r->server->entry.max_life)
1911 t = start + min(t - start, *r->server->entry.max_life);
1912 #if 0
1913 t = min(t, start + realm->max_life);
1914 #endif
1915 r->et.endtime = t;
1916 if(f.renewable_ok && r->et.endtime < *b->till){
1917 f.renewable = 1;
1918 if(b->rtime == NULL){
1919 ALLOC(b->rtime);
1920 *b->rtime = 0;
1922 if(*b->rtime < *b->till)
1923 *b->rtime = *b->till;
1925 if(f.renewable && b->rtime){
1926 t = *b->rtime;
1927 if(t == 0)
1928 t = MAX_TIME;
1929 if(r->client->entry.max_renew)
1930 t = start + min(t - start, *r->client->entry.max_renew);
1931 if(r->server->entry.max_renew)
1932 t = start + min(t - start, *r->server->entry.max_renew);
1933 #if 0
1934 t = min(t, start + realm->max_renew);
1935 #endif
1936 ALLOC(r->et.renew_till);
1937 *r->et.renew_till = t;
1938 r->et.flags.renewable = 1;
1942 if (f.request_anonymous)
1943 r->et.flags.anonymous = 1;
1945 if(b->addresses){
1946 ALLOC(r->et.caddr);
1947 copy_HostAddresses(b->addresses, r->et.caddr);
1950 r->et.transited.tr_type = DOMAIN_X500_COMPRESS;
1951 krb5_data_zero(&r->et.transited.contents);
1953 /* The MIT ASN.1 library (obviously) doesn't tell lengths encoded
1954 * as 0 and as 0x80 (meaning indefinite length) apart, and is thus
1955 * incapable of correctly decoding SEQUENCE OF's of zero length.
1957 * To fix this, always send at least one no-op last_req
1959 * If there's a pw_end or valid_end we will use that,
1960 * otherwise just a dummy lr.
1962 r->ek.last_req.val = malloc(2 * sizeof(*r->ek.last_req.val));
1963 if (r->ek.last_req.val == NULL) {
1964 ret = ENOMEM;
1965 goto out;
1967 r->ek.last_req.len = 0;
1968 if (r->client->entry.pw_end
1969 && (config->kdc_warn_pwexpire == 0
1970 || kdc_time + config->kdc_warn_pwexpire >= *r->client->entry.pw_end)) {
1971 r->ek.last_req.val[r->ek.last_req.len].lr_type = LR_PW_EXPTIME;
1972 r->ek.last_req.val[r->ek.last_req.len].lr_value = *r->client->entry.pw_end;
1973 ++r->ek.last_req.len;
1975 if (r->client->entry.valid_end) {
1976 r->ek.last_req.val[r->ek.last_req.len].lr_type = LR_ACCT_EXPTIME;
1977 r->ek.last_req.val[r->ek.last_req.len].lr_value = *r->client->entry.valid_end;
1978 ++r->ek.last_req.len;
1980 if (r->ek.last_req.len == 0) {
1981 r->ek.last_req.val[r->ek.last_req.len].lr_type = LR_NONE;
1982 r->ek.last_req.val[r->ek.last_req.len].lr_value = 0;
1983 ++r->ek.last_req.len;
1985 r->ek.nonce = b->nonce;
1986 if (r->client->entry.valid_end || r->client->entry.pw_end) {
1987 ALLOC(r->ek.key_expiration);
1988 if (r->client->entry.valid_end) {
1989 if (r->client->entry.pw_end)
1990 *r->ek.key_expiration = min(*r->client->entry.valid_end,
1991 *r->client->entry.pw_end);
1992 else
1993 *r->ek.key_expiration = *r->client->entry.valid_end;
1994 } else
1995 *r->ek.key_expiration = *r->client->entry.pw_end;
1996 } else
1997 r->ek.key_expiration = NULL;
1998 r->ek.flags = r->et.flags;
1999 r->ek.authtime = r->et.authtime;
2000 if (r->et.starttime) {
2001 ALLOC(r->ek.starttime);
2002 *r->ek.starttime = *r->et.starttime;
2004 r->ek.endtime = r->et.endtime;
2005 if (r->et.renew_till) {
2006 ALLOC(r->ek.renew_till);
2007 *r->ek.renew_till = *r->et.renew_till;
2009 copy_Realm(&rep.ticket.realm, &r->ek.srealm);
2010 copy_PrincipalName(&rep.ticket.sname, &r->ek.sname);
2011 if(r->et.caddr){
2012 ALLOC(r->ek.caddr);
2013 copy_HostAddresses(r->et.caddr, r->ek.caddr);
2017 * Check and session and reply keys
2020 if (r->session_key.keytype == ETYPE_NULL) {
2021 ret = krb5_generate_random_keyblock(context, r->sessionetype, &r->session_key);
2022 if (ret)
2023 goto out;
2026 if (r->reply_key.keytype == ETYPE_NULL) {
2027 _kdc_set_e_text(r, "Client have no reply key");
2028 ret = KRB5KDC_ERR_CLIENT_NOTYET;
2029 goto out;
2032 ret = copy_EncryptionKey(&r->session_key, &r->et.key);
2033 if (ret)
2034 goto out;
2036 ret = copy_EncryptionKey(&r->session_key, &r->ek.key);
2037 if (ret)
2038 goto out;
2041 * Add signing of alias referral
2044 if (f.canonicalize) {
2045 PA_ClientCanonicalized canon;
2046 krb5_data data;
2047 PA_DATA tmppa;
2048 krb5_crypto cryptox;
2049 size_t len = 0;
2051 memset(&canon, 0, sizeof(canon));
2053 canon.names.requested_name = *b->cname;
2054 canon.names.mapped_name = r->client->entry.principal->name;
2056 ASN1_MALLOC_ENCODE(PA_ClientCanonicalizedNames, data.data, data.length,
2057 &canon.names, &len, ret);
2058 if (ret)
2059 goto out;
2060 if (data.length != len)
2061 krb5_abortx(context, "internal asn.1 error");
2063 /* sign using "returned session key" */
2064 ret = krb5_crypto_init(context, &r->et.key, 0, &cryptox);
2065 if (ret) {
2066 free(data.data);
2067 goto out;
2070 ret = krb5_create_checksum(context, cryptox,
2071 KRB5_KU_CANONICALIZED_NAMES, 0,
2072 data.data, data.length,
2073 &canon.canon_checksum);
2074 free(data.data);
2075 krb5_crypto_destroy(context, cryptox);
2076 if (ret)
2077 goto out;
2079 ASN1_MALLOC_ENCODE(PA_ClientCanonicalized, data.data, data.length,
2080 &canon, &len, ret);
2081 free_Checksum(&canon.canon_checksum);
2082 if (ret)
2083 goto out;
2084 if (data.length != len)
2085 krb5_abortx(context, "internal asn.1 error");
2087 tmppa.padata_type = KRB5_PADATA_CLIENT_CANONICALIZED;
2088 tmppa.padata_value = data;
2089 ret = add_METHOD_DATA(&r->outpadata, &tmppa);
2090 free(data.data);
2091 if (ret)
2092 goto out;
2095 if (r->outpadata.len) {
2097 ALLOC(rep.padata);
2098 if (rep.padata == NULL) {
2099 ret = ENOMEM;
2100 goto out;
2102 ret = copy_METHOD_DATA(&r->outpadata, rep.padata);
2103 if (ret)
2104 goto out;
2107 /* Add the PAC */
2108 if (send_pac_p(context, req)) {
2109 generate_pac(r, skey);
2112 _kdc_log_timestamp(context, config, "AS-REQ", r->et.authtime, r->et.starttime,
2113 r->et.endtime, r->et.renew_till);
2115 /* do this as the last thing since this signs the EncTicketPart */
2116 ret = _kdc_add_KRB5SignedPath(context,
2117 config,
2118 r->server,
2119 setype,
2120 r->client->entry.principal,
2121 NULL,
2122 NULL,
2123 &r->et);
2124 if (ret)
2125 goto out;
2127 log_as_req(context, config, r->reply_key.keytype, setype, b);
2130 * Add REQ_ENC_PA_REP if client supports it
2133 i = 0;
2134 pa = _kdc_find_padata(req, &i, KRB5_PADATA_REQ_ENC_PA_REP);
2135 if (pa) {
2137 ret = add_enc_pa_rep(r);
2138 if (ret) {
2139 const char *msg = krb5_get_error_message(r->context, ret);
2140 _kdc_r_log(r, 0, "add_enc_pa_rep failed: %s: %d", msg, ret);
2141 krb5_free_error_message(r->context, msg);
2142 goto out;
2150 ret = _kdc_encode_reply(context, config,
2151 r->armor_crypto, req->req_body.nonce,
2152 &rep, &r->et, &r->ek, setype, r->server->entry.kvno,
2153 &skey->key, r->client->entry.kvno,
2154 &r->reply_key, 0, &r->e_text, reply);
2155 if (ret)
2156 goto out;
2159 * Check if message too large
2161 if (datagram_reply && reply->length > config->max_datagram_reply_length) {
2162 krb5_data_free(reply);
2163 ret = KRB5KRB_ERR_RESPONSE_TOO_BIG;
2164 _kdc_set_e_text(r, "Reply packet too large");
2167 out:
2168 free_AS_REP(&rep);
2171 * In case of a non proxy error, build an error message.
2173 if(ret != 0 && ret != HDB_ERR_NOT_FOUND_HERE) {
2174 ret = _kdc_fast_mk_error(context, r,
2175 &error_method,
2176 r->armor_crypto,
2177 &req->req_body,
2178 ret, r->e_text,
2179 r->client_princ, r->server_princ,
2180 NULL, NULL,
2181 reply);
2182 if (ret)
2183 goto out2;
2185 out2:
2186 free_EncTicketPart(&r->et);
2187 free_EncKDCRepPart(&r->ek);
2188 free_KDCFastState(&r->fast);
2190 if (error_method.len)
2191 free_METHOD_DATA(&error_method);
2192 if (r->outpadata.len)
2193 free_METHOD_DATA(&r->outpadata);
2194 if (r->client_princ) {
2195 krb5_free_principal(context, r->client_princ);
2196 r->client_princ = NULL;
2198 if (r->client_name) {
2199 free(r->client_name);
2200 r->client_name = NULL;
2202 if (r->server_princ){
2203 krb5_free_principal(context, r->server_princ);
2204 r->server_princ = NULL;
2206 if (r->server_name) {
2207 free(r->server_name);
2208 r->server_name = NULL;
2210 if (r->client)
2211 _kdc_free_ent(context, r->client);
2212 if (r->server)
2213 _kdc_free_ent(context, r->server);
2214 if (r->armor_crypto) {
2215 krb5_crypto_destroy(r->context, r->armor_crypto);
2216 r->armor_crypto = NULL;
2218 krb5_free_keyblock_contents(r->context, &r->reply_key);
2219 krb5_free_keyblock_contents(r->context, &r->session_key);
2220 return ret;
2224 * Add the AuthorizationData `data´ of `type´ to the last element in
2225 * the sequence of authorization_data in `tkt´ wrapped in an IF_RELEVANT
2228 krb5_error_code
2229 _kdc_tkt_add_if_relevant_ad(krb5_context context,
2230 EncTicketPart *tkt,
2231 int type,
2232 const krb5_data *data)
2234 krb5_error_code ret;
2235 size_t size = 0;
2237 if (tkt->authorization_data == NULL) {
2238 tkt->authorization_data = calloc(1, sizeof(*tkt->authorization_data));
2239 if (tkt->authorization_data == NULL) {
2240 krb5_set_error_message(context, ENOMEM, "out of memory");
2241 return ENOMEM;
2245 /* add the entry to the last element */
2247 AuthorizationData ad = { 0, NULL };
2248 AuthorizationDataElement ade;
2250 ade.ad_type = type;
2251 ade.ad_data = *data;
2253 ret = add_AuthorizationData(&ad, &ade);
2254 if (ret) {
2255 krb5_set_error_message(context, ret, "add AuthorizationData failed");
2256 return ret;
2259 ade.ad_type = KRB5_AUTHDATA_IF_RELEVANT;
2261 ASN1_MALLOC_ENCODE(AuthorizationData,
2262 ade.ad_data.data, ade.ad_data.length,
2263 &ad, &size, ret);
2264 free_AuthorizationData(&ad);
2265 if (ret) {
2266 krb5_set_error_message(context, ret, "ASN.1 encode of "
2267 "AuthorizationData failed");
2268 return ret;
2270 if (ade.ad_data.length != size)
2271 krb5_abortx(context, "internal asn.1 encoder error");
2273 ret = add_AuthorizationData(tkt->authorization_data, &ade);
2274 der_free_octet_string(&ade.ad_data);
2275 if (ret) {
2276 krb5_set_error_message(context, ret, "add AuthorizationData failed");
2277 return ret;
2281 return 0;