copy news from release branch
[heimdal.git] / kdc / kerberos5.c
blob6b6ea7d49b327a7d7be0df641a9f80879312f48d
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 = (krb5_enctype)ETYPE_NULL;
134 const krb5_enctype *p;
135 Key *key = NULL;
136 int i, k;
138 /* We'll want to avoid keys with v4 salted keys in the pre-auth case... */
139 ret = krb5_get_pw_salt(context, princ->entry.principal, &def_salt);
140 if (ret)
141 return ret;
143 ret = KRB5KDC_ERR_ETYPE_NOSUPP;
145 if (use_strongest_session_key) {
148 * Pick the strongest key that the KDC, target service, and
149 * client all support, using the local cryptosystem enctype
150 * list in strongest-to-weakest order to drive the search.
152 * This is not what RFC4120 says to do, but it encourages
153 * adoption of stronger enctypes. This doesn't play well with
154 * clients that have multiple Kerberos client implementations
155 * available with different supported enctype lists.
158 /* drive the search with local supported enctypes list */
159 p = krb5_kerberos_enctypes(context);
160 for (i = 0;
161 p[i] != (krb5_enctype)ETYPE_NULL && enctype == (krb5_enctype)ETYPE_NULL;
162 i++) {
163 if (krb5_enctype_valid(context, p[i]) != 0 &&
164 !_kdc_is_weak_exception(princ->entry.principal, p[i]))
165 continue;
167 /* check that the client supports it too */
168 for (k = 0; k < len && enctype == (krb5_enctype)ETYPE_NULL; k++) {
170 if (p[i] != etypes[k])
171 continue;
173 /* check target princ support */
174 key = NULL;
175 while (hdb_next_enctype2key(context, &princ->entry, NULL,
176 p[i], &key) == 0) {
177 if (key->key.keyvalue.length == 0) {
178 ret = KRB5KDC_ERR_NULL_KEY;
179 continue;
181 enctype = p[i];
182 ret = 0;
183 if (is_preauth && ret_key != NULL &&
184 !is_default_salt_p(&def_salt, key))
185 continue;
189 } else {
191 * Pick the first key from the client's enctype list that is
192 * supported by the cryptosystem and by the given principal.
194 * RFC4120 says we SHOULD pick the first _strong_ key from the
195 * client's list... not the first key... If the admin disallows
196 * weak enctypes in krb5.conf and selects this key selection
197 * algorithm, then we get exactly what RFC4120 says.
199 for(i = 0; ret != 0 && i < len; i++) {
201 if (krb5_enctype_valid(context, etypes[i]) != 0 &&
202 !_kdc_is_weak_exception(princ->entry.principal, etypes[i]))
203 continue;
205 key = NULL;
206 while (ret != 0 &&
207 hdb_next_enctype2key(context, &princ->entry, NULL,
208 etypes[i], &key) == 0) {
209 if (key->key.keyvalue.length == 0) {
210 ret = KRB5KDC_ERR_NULL_KEY;
211 continue;
213 enctype = etypes[i];
214 ret = 0;
215 if (is_preauth && ret_key != NULL &&
216 !is_default_salt_p(&def_salt, key))
217 continue;
222 if (enctype == (krb5_enctype)ETYPE_NULL) {
224 * if the service principal is one for which there is a known 1DES
225 * exception and no other enctype matches both the client request and
226 * the service key list, provide a DES-CBC-CRC key.
228 if (ret_key == NULL &&
229 _kdc_is_weak_exception(princ->entry.principal, ETYPE_DES_CBC_CRC)) {
230 ret = 0;
231 enctype = ETYPE_DES_CBC_CRC;
232 } else {
233 ret = KRB5KDC_ERR_ETYPE_NOSUPP;
237 if (ret == 0) {
238 if (ret_enctype != NULL)
239 *ret_enctype = enctype;
240 if (ret_key != NULL)
241 *ret_key = key;
244 krb5_free_salt (context, def_salt);
245 return ret;
248 krb5_error_code
249 _kdc_make_anonymous_principalname (PrincipalName *pn)
251 pn->name_type = KRB5_NT_PRINCIPAL;
252 pn->name_string.len = 1;
253 pn->name_string.val = malloc(sizeof(*pn->name_string.val));
254 if (pn->name_string.val == NULL)
255 return ENOMEM;
256 pn->name_string.val[0] = strdup("anonymous");
257 if (pn->name_string.val[0] == NULL) {
258 free(pn->name_string.val);
259 pn->name_string.val = NULL;
260 return ENOMEM;
262 return 0;
265 static void
266 _kdc_r_log(kdc_request_t r, int level, const char *fmt, ...)
268 va_list ap;
269 char *s;
270 va_start(ap, fmt);
271 s = kdc_log_msg_va(r->context, r->config, level, fmt, ap);
272 if(s) free(s);
273 va_end(ap);
276 static void
277 _kdc_set_e_text(kdc_request_t r, const char *e_text)
279 r->e_text = e_text;
280 kdc_log(r->context, r->config, 0, "%s", e_text);
283 void
284 _kdc_log_timestamp(krb5_context context,
285 krb5_kdc_configuration *config,
286 const char *type,
287 KerberosTime authtime, KerberosTime *starttime,
288 KerberosTime endtime, KerberosTime *renew_till)
290 char authtime_str[100], starttime_str[100],
291 endtime_str[100], renewtime_str[100];
293 krb5_format_time(context, authtime,
294 authtime_str, sizeof(authtime_str), TRUE);
295 if (starttime)
296 krb5_format_time(context, *starttime,
297 starttime_str, sizeof(starttime_str), TRUE);
298 else
299 strlcpy(starttime_str, "unset", sizeof(starttime_str));
300 krb5_format_time(context, endtime,
301 endtime_str, sizeof(endtime_str), TRUE);
302 if (renew_till)
303 krb5_format_time(context, *renew_till,
304 renewtime_str, sizeof(renewtime_str), TRUE);
305 else
306 strlcpy(renewtime_str, "unset", sizeof(renewtime_str));
308 kdc_log(context, config, 5,
309 "%s authtime: %s starttime: %s endtime: %s renew till: %s",
310 type, authtime_str, starttime_str, endtime_str, renewtime_str);
317 #ifdef PKINIT
319 static krb5_error_code
320 pa_pkinit_validate(kdc_request_t r, const PA_DATA *pa)
322 pk_client_params *pkp = NULL;
323 char *client_cert = NULL;
324 krb5_error_code ret;
326 ret = _kdc_pk_rd_padata(r->context, r->config, &r->req, pa, r->client, &pkp);
327 if (ret || pkp == NULL) {
328 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
329 _kdc_r_log(r, 5, "Failed to decode PKINIT PA-DATA -- %s",
330 r->client_name);
331 goto out;
334 ret = _kdc_pk_check_client(r->context,
335 r->config,
336 r->clientdb,
337 r->client,
338 pkp,
339 &client_cert);
340 if (ret) {
341 _kdc_set_e_text(r, "PKINIT certificate not allowed to "
342 "impersonate principal");
343 goto out;
346 _kdc_r_log(r, 0, "PKINIT pre-authentication succeeded -- %s using %s",
347 r->client_name, client_cert);
348 free(client_cert);
350 ret = _kdc_pk_mk_pa_reply(r->context, r->config, pkp, r->client,
351 r->sessionetype, &r->req, &r->request,
352 &r->reply_key, &r->session_key, &r->outpadata);
353 if (ret) {
354 _kdc_set_e_text(r, "Failed to build PK-INIT reply");
355 goto out;
357 #if 0
358 ret = _kdc_add_inital_verified_cas(r->context, r->config,
359 pkp, &r->et);
360 #endif
361 out:
362 if (pkp)
363 _kdc_pk_free_client_param(r->context, pkp);
365 return ret;
368 #endif /* PKINIT */
374 static krb5_error_code
375 make_pa_enc_challange(krb5_context context, METHOD_DATA *md,
376 krb5_crypto crypto)
378 PA_ENC_TS_ENC p;
379 unsigned char *buf;
380 size_t buf_size;
381 size_t len;
382 EncryptedData encdata;
383 krb5_error_code ret;
384 int32_t usec;
385 int usec2;
387 krb5_us_timeofday (context, &p.patimestamp, &usec);
388 usec2 = usec;
389 p.pausec = &usec2;
391 ASN1_MALLOC_ENCODE(PA_ENC_TS_ENC, buf, buf_size, &p, &len, ret);
392 if (ret)
393 return ret;
394 if(buf_size != len)
395 krb5_abortx(context, "internal error in ASN.1 encoder");
397 ret = krb5_encrypt_EncryptedData(context,
398 crypto,
399 KRB5_KU_ENC_CHALLENGE_KDC,
400 buf,
401 len,
403 &encdata);
404 free(buf);
405 if (ret)
406 return ret;
408 ASN1_MALLOC_ENCODE(EncryptedData, buf, buf_size, &encdata, &len, ret);
409 free_EncryptedData(&encdata);
410 if (ret)
411 return ret;
412 if(buf_size != len)
413 krb5_abortx(context, "internal error in ASN.1 encoder");
415 ret = krb5_padata_add(context, md, KRB5_PADATA_ENCRYPTED_CHALLENGE, buf, len);
416 if (ret)
417 free(buf);
418 return ret;
421 static krb5_error_code
422 pa_enc_chal_validate(kdc_request_t r, const PA_DATA *pa)
424 krb5_data pepper1, pepper2, ts_data;
425 KDC_REQ_BODY *b = &r->req.req_body;
426 EncryptedData enc_data;
427 krb5_enctype aenctype;
428 krb5_error_code ret;
429 struct Key *k;
430 size_t size;
431 int i;
433 heim_assert(r->armor_crypto != NULL, "ENC-CHAL called for non FAST");
435 if (b->kdc_options.request_anonymous) {
436 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
437 kdc_log(r->context, r->config, 0, "ENC-CHALL doesn't support anon");
438 return ret;
441 ret = decode_EncryptedData(pa->padata_value.data,
442 pa->padata_value.length,
443 &enc_data,
444 &size);
445 if (ret) {
446 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
447 _kdc_r_log(r, 5, "Failed to decode PA-DATA -- %s",
448 r->client_name);
449 return ret;
452 pepper1.data = "clientchallengearmor";
453 pepper1.length = strlen(pepper1.data);
454 pepper2.data = "challengelongterm";
455 pepper2.length = strlen(pepper2.data);
457 krb5_crypto_getenctype(r->context, r->armor_crypto, &aenctype);
459 for (i = 0; i < r->client->entry.keys.len; i++) {
460 krb5_crypto challangecrypto, longtermcrypto;
461 krb5_keyblock challangekey;
462 PA_ENC_TS_ENC p;
464 k = &r->client->entry.keys.val[i];
466 ret = krb5_crypto_init(r->context, &k->key, 0, &longtermcrypto);
467 if (ret)
468 continue;
470 ret = krb5_crypto_fx_cf2(r->context, r->armor_crypto, longtermcrypto,
471 &pepper1, &pepper2, aenctype,
472 &challangekey);
473 krb5_crypto_destroy(r->context, longtermcrypto);
474 if (ret)
475 continue;
477 ret = krb5_crypto_init(r->context, &challangekey, 0,
478 &challangecrypto);
479 if (ret)
480 continue;
482 ret = krb5_decrypt_EncryptedData(r->context, challangecrypto,
483 KRB5_KU_ENC_CHALLENGE_CLIENT,
484 &enc_data,
485 &ts_data);
486 if (ret)
487 continue;
489 ret = decode_PA_ENC_TS_ENC(ts_data.data,
490 ts_data.length,
492 &size);
493 krb5_data_free(&ts_data);
494 if(ret){
495 krb5_crypto_destroy(r->context, challangecrypto);
496 ret = KRB5KDC_ERR_PREAUTH_FAILED;
497 _kdc_r_log(r, 5, "Failed to decode PA-ENC-TS_ENC -- %s",
498 r->client_name);
499 continue;
502 if (abs(kdc_time - p.patimestamp) > r->context->max_skew) {
503 char client_time[100];
505 krb5_crypto_destroy(r->context, challangecrypto);
507 krb5_format_time(r->context, p.patimestamp,
508 client_time, sizeof(client_time), TRUE);
510 ret = KRB5KRB_AP_ERR_SKEW;
511 _kdc_r_log(r, 0, "Too large time skew, "
512 "client time %s is out by %u > %u seconds -- %s",
513 client_time,
514 (unsigned)abs(kdc_time - p.patimestamp),
515 r->context->max_skew,
516 r->client_name);
518 free_PA_ENC_TS_ENC(&p);
519 goto out;
522 free_PA_ENC_TS_ENC(&p);
524 ret = make_pa_enc_challange(r->context, &r->outpadata,
525 challangecrypto);
526 krb5_crypto_destroy(r->context, challangecrypto);
527 if (ret)
528 goto out;
530 set_salt_padata(&r->outpadata, k->salt);
531 krb5_free_keyblock_contents(r->context, &r->reply_key);
532 ret = krb5_copy_keyblock_contents(r->context, &k->key, &r->reply_key);
533 if (ret)
534 goto out;
536 break;
538 if (i < r->client->entry.keys.len)
539 ret = KRB5KDC_ERR_PREAUTH_FAILED;
540 out:
541 free_EncryptedData(&enc_data);
543 return ret;
546 static krb5_error_code
547 pa_enc_ts_validate(kdc_request_t r, const PA_DATA *pa)
549 EncryptedData enc_data;
550 krb5_error_code ret;
551 krb5_crypto crypto;
552 krb5_data ts_data;
553 PA_ENC_TS_ENC p;
554 size_t len;
555 Key *pa_key;
556 char *str;
558 if (r->req.req_body.kdc_options.request_anonymous) {
559 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
560 _kdc_set_e_text(r, "ENC-TS doesn't support anon");
561 goto out;
564 ret = decode_EncryptedData(pa->padata_value.data,
565 pa->padata_value.length,
566 &enc_data,
567 &len);
568 if (ret) {
569 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
570 _kdc_r_log(r, 5, "Failed to decode PA-DATA -- %s",
571 r->client_name);
572 goto out;
575 ret = hdb_enctype2key(r->context, &r->client->entry, NULL,
576 enc_data.etype, &pa_key);
577 if(ret){
578 char *estr;
579 _kdc_set_e_text(r, "No key matching entype");
580 ret = KRB5KDC_ERR_ETYPE_NOSUPP;
581 if(krb5_enctype_to_string(r->context, enc_data.etype, &estr))
582 estr = NULL;
583 if(estr == NULL)
584 _kdc_r_log(r, 5,
585 "No client key matching pa-data (%d) -- %s",
586 enc_data.etype, r->client_name);
587 else
588 _kdc_r_log(r, 5,
589 "No client key matching pa-data (%s) -- %s",
590 estr, r->client_name);
591 free(estr);
592 free_EncryptedData(&enc_data);
593 goto out;
596 try_next_key:
597 ret = krb5_crypto_init(r->context, &pa_key->key, 0, &crypto);
598 if (ret) {
599 const char *msg = krb5_get_error_message(r->context, ret);
600 _kdc_r_log(r, 0, "krb5_crypto_init failed: %s", msg);
601 krb5_free_error_message(r->context, msg);
602 free_EncryptedData(&enc_data);
603 goto out;
606 ret = krb5_decrypt_EncryptedData (r->context,
607 crypto,
608 KRB5_KU_PA_ENC_TIMESTAMP,
609 &enc_data,
610 &ts_data);
611 krb5_crypto_destroy(r->context, crypto);
613 * Since the user might have several keys with the same
614 * enctype but with diffrent salting, we need to try all
615 * the keys with the same enctype.
617 if(ret){
618 krb5_error_code ret2;
619 const char *msg = krb5_get_error_message(r->context, ret);
621 ret2 = krb5_enctype_to_string(r->context,
622 pa_key->key.keytype, &str);
623 if (ret2)
624 str = NULL;
625 _kdc_r_log(r, 5, "Failed to decrypt PA-DATA -- %s "
626 "(enctype %s) error %s",
627 r->client_name, str ? str : "unknown enctype", msg);
628 krb5_free_error_message(r->context, msg);
629 free(str);
631 if(hdb_next_enctype2key(r->context, &r->client->entry, NULL,
632 enc_data.etype, &pa_key) == 0)
633 goto try_next_key;
635 free_EncryptedData(&enc_data);
637 if (r->clientdb->hdb_auth_status)
638 r->clientdb->hdb_auth_status(r->context, r->clientdb, r->client,
639 HDB_AUTH_WRONG_PASSWORD);
641 ret = KRB5KDC_ERR_PREAUTH_FAILED;
642 goto out;
644 free_EncryptedData(&enc_data);
645 ret = decode_PA_ENC_TS_ENC(ts_data.data,
646 ts_data.length,
648 &len);
649 krb5_data_free(&ts_data);
650 if(ret){
651 ret = KRB5KDC_ERR_PREAUTH_FAILED;
652 _kdc_r_log(r, 5, "Failed to decode PA-ENC-TS_ENC -- %s",
653 r->client_name);
654 goto out;
656 if (abs(kdc_time - p.patimestamp) > r->context->max_skew) {
657 char client_time[100];
659 krb5_format_time(r->context, p.patimestamp,
660 client_time, sizeof(client_time), TRUE);
662 ret = KRB5KRB_AP_ERR_SKEW;
663 _kdc_r_log(r, 0, "Too large time skew, "
664 "client time %s is out by %u > %u seconds -- %s",
665 client_time,
666 (unsigned)abs(kdc_time - p.patimestamp),
667 r->context->max_skew,
668 r->client_name);
671 * The following is needed to make windows clients to
672 * retry using the timestamp in the error message, if
673 * there is a e_text, they become unhappy.
675 r->e_text = NULL;
676 free_PA_ENC_TS_ENC(&p);
677 goto out;
679 free_PA_ENC_TS_ENC(&p);
681 set_salt_padata(&r->outpadata, pa_key->salt);
683 ret = krb5_copy_keyblock_contents(r->context, &pa_key->key, &r->reply_key);
684 if (ret)
685 return ret;
687 ret = krb5_enctype_to_string(r->context, pa_key->key.keytype, &str);
688 if (ret)
689 str = NULL;
690 _kdc_r_log(r, 2, "ENC-TS Pre-authentication succeeded -- %s using %s",
691 r->client_name, str ? str : "unknown enctype");
692 free(str);
694 ret = 0;
696 out:
698 return ret;
701 struct kdc_patypes {
702 int type;
703 char *name;
704 unsigned int flags;
705 #define PA_ANNOUNCE 1
706 #define PA_REQ_FAST 2 /* only use inside fast */
707 krb5_error_code (*validate)(kdc_request_t, const PA_DATA *pa);
710 static const struct kdc_patypes pat[] = {
711 #ifdef PKINIT
713 KRB5_PADATA_PK_AS_REQ, "PK-INIT(ietf)", PA_ANNOUNCE,
714 pa_pkinit_validate
717 KRB5_PADATA_PK_AS_REQ_WIN, "PK-INIT(win2k)", PA_ANNOUNCE,
718 pa_pkinit_validate
720 #else
721 { KRB5_PADATA_PK_AS_REQ, "PK-INIT(ietf)", 0, NULL },
722 { KRB5_PADATA_PK_AS_REQ_WIN, "PK-INIT(win2k)", 0, NULL },
723 #endif
724 { KRB5_PADATA_PA_PK_OCSP_RESPONSE , "OCSP", 0, NULL },
726 KRB5_PADATA_ENC_TIMESTAMP , "ENC-TS",
727 PA_ANNOUNCE,
728 pa_enc_ts_validate
731 KRB5_PADATA_ENCRYPTED_CHALLENGE , "ENC-CHAL",
732 PA_ANNOUNCE | PA_REQ_FAST,
733 pa_enc_chal_validate
735 { KRB5_PADATA_REQ_ENC_PA_REP , "REQ-ENC-PA-REP", 0, NULL },
736 { KRB5_PADATA_FX_FAST, "FX-FAST", PA_ANNOUNCE, NULL },
737 { KRB5_PADATA_FX_ERROR, "FX-ERROR", 0, NULL },
738 { KRB5_PADATA_FX_COOKIE, "FX-COOKIE", 0, NULL }
741 static void
742 log_patypes(krb5_context context,
743 krb5_kdc_configuration *config,
744 METHOD_DATA *padata)
746 struct rk_strpool *p = NULL;
747 char *str;
748 size_t n, m;
750 for (n = 0; n < padata->len; n++) {
751 for (m = 0; m < sizeof(pat) / sizeof(pat[0]); m++) {
752 if (padata->val[n].padata_type == pat[m].type) {
753 p = rk_strpoolprintf(p, "%s", pat[m].name);
754 break;
757 if (m == sizeof(pat) / sizeof(pat[0]))
758 p = rk_strpoolprintf(p, "%d", padata->val[n].padata_type);
759 if (p && n + 1 < padata->len)
760 p = rk_strpoolprintf(p, ", ");
761 if (p == NULL) {
762 kdc_log(context, config, 0, "out of memory");
763 return;
766 if (p == NULL)
767 p = rk_strpoolprintf(p, "none");
769 str = rk_strpoolcollect(p);
770 kdc_log(context, config, 0, "Client sent patypes: %s", str);
771 free(str);
778 krb5_error_code
779 _kdc_encode_reply(krb5_context context,
780 krb5_kdc_configuration *config,
781 krb5_crypto armor_crypto, uint32_t nonce,
782 KDC_REP *rep, EncTicketPart *et, EncKDCRepPart *ek,
783 krb5_enctype etype,
784 int skvno, const EncryptionKey *skey,
785 int ckvno, const EncryptionKey *reply_key,
786 int rk_is_subkey,
787 const char **e_text,
788 krb5_data *reply)
790 unsigned char *buf;
791 size_t buf_size;
792 size_t len = 0;
793 krb5_error_code ret;
794 krb5_crypto crypto;
796 ASN1_MALLOC_ENCODE(EncTicketPart, buf, buf_size, et, &len, ret);
797 if(ret) {
798 const char *msg = krb5_get_error_message(context, ret);
799 kdc_log(context, config, 0, "Failed to encode ticket: %s", msg);
800 krb5_free_error_message(context, msg);
801 return ret;
803 if(buf_size != len)
804 krb5_abortx(context, "Internal error in ASN.1 encoder");
806 ret = krb5_crypto_init(context, skey, etype, &crypto);
807 if (ret) {
808 const char *msg = krb5_get_error_message(context, ret);
809 kdc_log(context, config, 0, "krb5_crypto_init failed: %s", msg);
810 krb5_free_error_message(context, msg);
811 return ret;
814 ret = krb5_encrypt_EncryptedData(context,
815 crypto,
816 KRB5_KU_TICKET,
817 buf,
818 len,
819 skvno,
820 &rep->ticket.enc_part);
821 free(buf);
822 krb5_crypto_destroy(context, crypto);
823 if(ret) {
824 const char *msg = krb5_get_error_message(context, ret);
825 kdc_log(context, config, 0, "Failed to encrypt data: %s", msg);
826 krb5_free_error_message(context, msg);
827 return ret;
830 if (armor_crypto) {
831 krb5_data data;
832 krb5_keyblock *strengthen_key = NULL;
833 KrbFastFinished finished;
835 kdc_log(context, config, 0, "FAST armor protection");
837 memset(&finished, 0, sizeof(finished));
838 krb5_data_zero(&data);
840 finished.timestamp = kdc_time;
841 finished.usec = 0;
842 finished.crealm = et->crealm;
843 finished.cname = et->cname;
845 ASN1_MALLOC_ENCODE(Ticket, data.data, data.length,
846 &rep->ticket, &len, ret);
847 if (ret)
848 return ret;
849 if (data.length != len)
850 krb5_abortx(context, "internal asn.1 error");
852 ret = krb5_create_checksum(context, armor_crypto,
853 KRB5_KU_FAST_FINISHED, 0,
854 data.data, data.length,
855 &finished.ticket_checksum);
856 krb5_data_free(&data);
857 if (ret)
858 return ret;
860 ret = _kdc_fast_mk_response(context, armor_crypto,
861 rep->padata, strengthen_key, &finished,
862 nonce, &data);
863 free_Checksum(&finished.ticket_checksum);
864 if (ret)
865 return ret;
867 if (rep->padata) {
868 free_METHOD_DATA(rep->padata);
869 } else {
870 rep->padata = calloc(1, sizeof(*(rep->padata)));
871 if (rep->padata == NULL) {
872 krb5_data_free(&data);
873 return ENOMEM;
877 ret = krb5_padata_add(context, rep->padata,
878 KRB5_PADATA_FX_FAST,
879 data.data, data.length);
880 if (ret)
881 return ret;
884 * Hide client name of privacy reasons
886 if (1 /* r->fast_options.hide_client_names */) {
887 rep->crealm[0] = '\0';
888 free_PrincipalName(&rep->cname);
889 rep->cname.name_type = 0;
893 if(rep->msg_type == krb_as_rep && !config->encode_as_rep_as_tgs_rep)
894 ASN1_MALLOC_ENCODE(EncASRepPart, buf, buf_size, ek, &len, ret);
895 else
896 ASN1_MALLOC_ENCODE(EncTGSRepPart, buf, buf_size, ek, &len, ret);
897 if(ret) {
898 const char *msg = krb5_get_error_message(context, ret);
899 kdc_log(context, config, 0, "Failed to encode KDC-REP: %s", msg);
900 krb5_free_error_message(context, msg);
901 return ret;
903 if(buf_size != len) {
904 free(buf);
905 kdc_log(context, config, 0, "Internal error in ASN.1 encoder");
906 *e_text = "KDC internal error";
907 return KRB5KRB_ERR_GENERIC;
909 ret = krb5_crypto_init(context, reply_key, 0, &crypto);
910 if (ret) {
911 const char *msg = krb5_get_error_message(context, ret);
912 free(buf);
913 kdc_log(context, config, 0, "krb5_crypto_init failed: %s", msg);
914 krb5_free_error_message(context, msg);
915 return ret;
917 if(rep->msg_type == krb_as_rep) {
918 krb5_encrypt_EncryptedData(context,
919 crypto,
920 KRB5_KU_AS_REP_ENC_PART,
921 buf,
922 len,
923 ckvno,
924 &rep->enc_part);
925 free(buf);
926 ASN1_MALLOC_ENCODE(AS_REP, buf, buf_size, rep, &len, ret);
927 } else {
928 krb5_encrypt_EncryptedData(context,
929 crypto,
930 rk_is_subkey ? KRB5_KU_TGS_REP_ENC_PART_SUB_KEY : KRB5_KU_TGS_REP_ENC_PART_SESSION,
931 buf,
932 len,
933 ckvno,
934 &rep->enc_part);
935 free(buf);
936 ASN1_MALLOC_ENCODE(TGS_REP, buf, buf_size, rep, &len, ret);
938 krb5_crypto_destroy(context, crypto);
939 if(ret) {
940 const char *msg = krb5_get_error_message(context, ret);
941 kdc_log(context, config, 0, "Failed to encode KDC-REP: %s", msg);
942 krb5_free_error_message(context, msg);
943 return ret;
945 if(buf_size != len) {
946 free(buf);
947 kdc_log(context, config, 0, "Internal error in ASN.1 encoder");
948 *e_text = "KDC internal error";
949 return KRB5KRB_ERR_GENERIC;
951 reply->data = buf;
952 reply->length = buf_size;
953 return 0;
957 * Return 1 if the client have only older enctypes, this is for
958 * determining if the server should send ETYPE_INFO2 or not.
961 static int
962 older_enctype(krb5_enctype enctype)
964 switch (enctype) {
965 case ETYPE_DES_CBC_CRC:
966 case ETYPE_DES_CBC_MD4:
967 case ETYPE_DES_CBC_MD5:
968 case ETYPE_DES3_CBC_SHA1:
969 case ETYPE_ARCFOUR_HMAC_MD5:
970 case ETYPE_ARCFOUR_HMAC_MD5_56:
972 * The following three is "old" windows enctypes and is needed for
973 * windows 2000 hosts.
975 case ETYPE_ARCFOUR_MD4:
976 case ETYPE_ARCFOUR_HMAC_OLD:
977 case ETYPE_ARCFOUR_HMAC_OLD_EXP:
978 return 1;
979 default:
980 return 0;
988 static krb5_error_code
989 make_etype_info_entry(krb5_context context, ETYPE_INFO_ENTRY *ent, Key *key)
991 ent->etype = key->key.keytype;
992 if(key->salt){
993 #if 0
994 ALLOC(ent->salttype);
996 if(key->salt->type == hdb_pw_salt)
997 *ent->salttype = 0; /* or 1? or NULL? */
998 else if(key->salt->type == hdb_afs3_salt)
999 *ent->salttype = 2;
1000 else {
1001 kdc_log(context, config, 0, "unknown salt-type: %d",
1002 key->salt->type);
1003 return KRB5KRB_ERR_GENERIC;
1005 /* according to `the specs', we can't send a salt if
1006 we have AFS3 salted key, but that requires that you
1007 *know* what cell you are using (e.g by assuming
1008 that the cell is the same as the realm in lower
1009 case) */
1010 #elif 0
1011 ALLOC(ent->salttype);
1012 *ent->salttype = key->salt->type;
1013 #else
1015 * We shouldn't sent salttype since it is incompatible with the
1016 * specification and it breaks windows clients. The afs
1017 * salting problem is solved by using KRB5-PADATA-AFS3-SALT
1018 * implemented in Heimdal 0.7 and later.
1020 ent->salttype = NULL;
1021 #endif
1022 krb5_copy_data(context, &key->salt->salt,
1023 &ent->salt);
1024 } else {
1025 /* we return no salt type at all, as that should indicate
1026 * the default salt type and make everybody happy. some
1027 * systems (like w2k) dislike being told the salt type
1028 * here. */
1030 ent->salttype = NULL;
1031 ent->salt = NULL;
1033 return 0;
1036 static krb5_error_code
1037 get_pa_etype_info(krb5_context context,
1038 krb5_kdc_configuration *config,
1039 METHOD_DATA *md, Key *ckey)
1041 krb5_error_code ret = 0;
1042 ETYPE_INFO pa;
1043 unsigned char *buf;
1044 size_t len;
1047 pa.len = 1;
1048 pa.val = calloc(1, sizeof(pa.val[0]));
1049 if(pa.val == NULL)
1050 return ENOMEM;
1052 ret = make_etype_info_entry(context, &pa.val[0], ckey);
1053 if (ret) {
1054 free_ETYPE_INFO(&pa);
1055 return ret;
1058 ASN1_MALLOC_ENCODE(ETYPE_INFO, buf, len, &pa, &len, ret);
1059 free_ETYPE_INFO(&pa);
1060 if(ret)
1061 return ret;
1062 ret = realloc_method_data(md);
1063 if(ret) {
1064 free(buf);
1065 return ret;
1067 md->val[md->len - 1].padata_type = KRB5_PADATA_ETYPE_INFO;
1068 md->val[md->len - 1].padata_value.length = len;
1069 md->val[md->len - 1].padata_value.data = buf;
1070 return 0;
1077 extern int _krb5_AES_string_to_default_iterator;
1079 static krb5_error_code
1080 make_etype_info2_entry(ETYPE_INFO2_ENTRY *ent, Key *key)
1082 ent->etype = key->key.keytype;
1083 if(key->salt) {
1084 ALLOC(ent->salt);
1085 if (ent->salt == NULL)
1086 return ENOMEM;
1087 *ent->salt = malloc(key->salt->salt.length + 1);
1088 if (*ent->salt == NULL) {
1089 free(ent->salt);
1090 ent->salt = NULL;
1091 return ENOMEM;
1093 memcpy(*ent->salt, key->salt->salt.data, key->salt->salt.length);
1094 (*ent->salt)[key->salt->salt.length] = '\0';
1095 } else
1096 ent->salt = NULL;
1098 ent->s2kparams = NULL;
1100 switch (key->key.keytype) {
1101 case ETYPE_AES128_CTS_HMAC_SHA1_96:
1102 case ETYPE_AES256_CTS_HMAC_SHA1_96:
1103 ALLOC(ent->s2kparams);
1104 if (ent->s2kparams == NULL)
1105 return ENOMEM;
1106 ent->s2kparams->length = 4;
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,
1114 _krb5_AES_string_to_default_iterator,
1115 ent->s2kparams->length);
1116 break;
1117 case ETYPE_DES_CBC_CRC:
1118 case ETYPE_DES_CBC_MD4:
1119 case ETYPE_DES_CBC_MD5:
1120 /* Check if this was a AFS3 salted key */
1121 if(key->salt && key->salt->type == hdb_afs3_salt){
1122 ALLOC(ent->s2kparams);
1123 if (ent->s2kparams == NULL)
1124 return ENOMEM;
1125 ent->s2kparams->length = 1;
1126 ent->s2kparams->data = malloc(ent->s2kparams->length);
1127 if (ent->s2kparams->data == NULL) {
1128 free(ent->s2kparams);
1129 ent->s2kparams = NULL;
1130 return ENOMEM;
1132 _krb5_put_int(ent->s2kparams->data,
1134 ent->s2kparams->length);
1136 break;
1137 default:
1138 break;
1140 return 0;
1144 * Return an ETYPE-INFO2. Enctypes are storted the same way as in the
1145 * database (client supported enctypes first, then the unsupported
1146 * enctypes).
1149 static krb5_error_code
1150 get_pa_etype_info2(krb5_context context,
1151 krb5_kdc_configuration *config,
1152 METHOD_DATA *md, Key *ckey)
1154 krb5_error_code ret = 0;
1155 ETYPE_INFO2 pa;
1156 unsigned char *buf;
1157 size_t len;
1159 pa.len = 1;
1160 pa.val = calloc(1, sizeof(pa.val[0]));
1161 if(pa.val == NULL)
1162 return ENOMEM;
1164 ret = make_etype_info2_entry(&pa.val[0], ckey);
1165 if (ret) {
1166 free_ETYPE_INFO2(&pa);
1167 return ret;
1170 ASN1_MALLOC_ENCODE(ETYPE_INFO2, buf, len, &pa, &len, ret);
1171 free_ETYPE_INFO2(&pa);
1172 if(ret)
1173 return ret;
1174 ret = realloc_method_data(md);
1175 if(ret) {
1176 free(buf);
1177 return ret;
1179 md->val[md->len - 1].padata_type = KRB5_PADATA_ETYPE_INFO2;
1180 md->val[md->len - 1].padata_value.length = len;
1181 md->val[md->len - 1].padata_value.data = buf;
1182 return 0;
1189 static void
1190 log_as_req(krb5_context context,
1191 krb5_kdc_configuration *config,
1192 krb5_enctype cetype,
1193 krb5_enctype setype,
1194 const KDC_REQ_BODY *b)
1196 krb5_error_code ret;
1197 struct rk_strpool *p;
1198 char *str;
1199 size_t i;
1201 p = rk_strpoolprintf(NULL, "%s", "Client supported enctypes: ");
1203 for (i = 0; i < b->etype.len; i++) {
1204 ret = krb5_enctype_to_string(context, b->etype.val[i], &str);
1205 if (ret == 0) {
1206 p = rk_strpoolprintf(p, "%s", str);
1207 free(str);
1208 } else
1209 p = rk_strpoolprintf(p, "%d", b->etype.val[i]);
1210 if (p && i + 1 < b->etype.len)
1211 p = rk_strpoolprintf(p, ", ");
1212 if (p == NULL) {
1213 kdc_log(context, config, 0, "out of memory");
1214 return;
1217 if (p == NULL)
1218 p = rk_strpoolprintf(p, "no encryption types");
1221 char *cet;
1222 char *set;
1224 ret = krb5_enctype_to_string(context, cetype, &cet);
1225 if(ret == 0) {
1226 ret = krb5_enctype_to_string(context, setype, &set);
1227 if (ret == 0) {
1228 p = rk_strpoolprintf(p, ", using %s/%s", cet, set);
1229 free(set);
1231 free(cet);
1233 if (ret != 0)
1234 p = rk_strpoolprintf(p, ", using enctypes %d/%d",
1235 cetype, setype);
1238 str = rk_strpoolcollect(p);
1239 kdc_log(context, config, 0, "%s", str);
1240 free(str);
1243 char fixedstr[128];
1244 unparse_flags(KDCOptions2int(b->kdc_options), asn1_KDCOptions_units(),
1245 fixedstr, sizeof(fixedstr));
1246 if(*fixedstr)
1247 kdc_log(context, config, 0, "Requested flags: %s", fixedstr);
1252 * verify the flags on `client' and `server', returning 0
1253 * if they are OK and generating an error messages and returning
1254 * and error code otherwise.
1257 krb5_error_code
1258 kdc_check_flags(krb5_context context,
1259 krb5_kdc_configuration *config,
1260 hdb_entry_ex *client_ex, const char *client_name,
1261 hdb_entry_ex *server_ex, const char *server_name,
1262 krb5_boolean is_as_req)
1264 if(client_ex != NULL) {
1265 hdb_entry *client = &client_ex->entry;
1267 /* check client */
1268 if (client->flags.locked_out) {
1269 kdc_log(context, config, 0,
1270 "Client (%s) is locked out", client_name);
1271 return KRB5KDC_ERR_POLICY;
1274 if (client->flags.invalid) {
1275 kdc_log(context, config, 0,
1276 "Client (%s) has invalid bit set", client_name);
1277 return KRB5KDC_ERR_POLICY;
1280 if(!client->flags.client){
1281 kdc_log(context, config, 0,
1282 "Principal may not act as client -- %s", client_name);
1283 return KRB5KDC_ERR_POLICY;
1286 if (client->valid_start && *client->valid_start > kdc_time) {
1287 char starttime_str[100];
1288 krb5_format_time(context, *client->valid_start,
1289 starttime_str, sizeof(starttime_str), TRUE);
1290 kdc_log(context, config, 0,
1291 "Client not yet valid until %s -- %s",
1292 starttime_str, client_name);
1293 return KRB5KDC_ERR_CLIENT_NOTYET;
1296 if (client->valid_end && *client->valid_end < kdc_time) {
1297 char endtime_str[100];
1298 krb5_format_time(context, *client->valid_end,
1299 endtime_str, sizeof(endtime_str), TRUE);
1300 kdc_log(context, config, 0,
1301 "Client expired at %s -- %s",
1302 endtime_str, client_name);
1303 return KRB5KDC_ERR_NAME_EXP;
1306 if (client->flags.require_pwchange &&
1307 (server_ex == NULL || !server_ex->entry.flags.change_pw)) {
1308 kdc_log(context, config, 0,
1309 "Client's key must be changed -- %s", client_name);
1310 return KRB5KDC_ERR_KEY_EXPIRED;
1313 if (client->pw_end && *client->pw_end < kdc_time
1314 && (server_ex == NULL || !server_ex->entry.flags.change_pw)) {
1315 char pwend_str[100];
1316 krb5_format_time(context, *client->pw_end,
1317 pwend_str, sizeof(pwend_str), TRUE);
1318 kdc_log(context, config, 0,
1319 "Client's key has expired at %s -- %s",
1320 pwend_str, client_name);
1321 return KRB5KDC_ERR_KEY_EXPIRED;
1325 /* check server */
1327 if (server_ex != NULL) {
1328 hdb_entry *server = &server_ex->entry;
1330 if (server->flags.locked_out) {
1331 kdc_log(context, config, 0,
1332 "Client server locked out -- %s", server_name);
1333 return KRB5KDC_ERR_POLICY;
1335 if (server->flags.invalid) {
1336 kdc_log(context, config, 0,
1337 "Server has invalid flag set -- %s", server_name);
1338 return KRB5KDC_ERR_POLICY;
1341 if(!server->flags.server){
1342 kdc_log(context, config, 0,
1343 "Principal may not act as server -- %s", server_name);
1344 return KRB5KDC_ERR_POLICY;
1347 if(!is_as_req && server->flags.initial) {
1348 kdc_log(context, config, 0,
1349 "AS-REQ is required for server -- %s", server_name);
1350 return KRB5KDC_ERR_POLICY;
1353 if (server->valid_start && *server->valid_start > kdc_time) {
1354 char starttime_str[100];
1355 krb5_format_time(context, *server->valid_start,
1356 starttime_str, sizeof(starttime_str), TRUE);
1357 kdc_log(context, config, 0,
1358 "Server not yet valid until %s -- %s",
1359 starttime_str, server_name);
1360 return KRB5KDC_ERR_SERVICE_NOTYET;
1363 if (server->valid_end && *server->valid_end < kdc_time) {
1364 char endtime_str[100];
1365 krb5_format_time(context, *server->valid_end,
1366 endtime_str, sizeof(endtime_str), TRUE);
1367 kdc_log(context, config, 0,
1368 "Server expired at %s -- %s",
1369 endtime_str, server_name);
1370 return KRB5KDC_ERR_SERVICE_EXP;
1373 if (server->pw_end && *server->pw_end < kdc_time) {
1374 char pwend_str[100];
1375 krb5_format_time(context, *server->pw_end,
1376 pwend_str, sizeof(pwend_str), TRUE);
1377 kdc_log(context, config, 0,
1378 "Server's key has expired at -- %s",
1379 pwend_str, server_name);
1380 return KRB5KDC_ERR_KEY_EXPIRED;
1383 return 0;
1387 * Return TRUE if `from' is part of `addresses' taking into consideration
1388 * the configuration variables that tells us how strict we should be about
1389 * these checks
1392 krb5_boolean
1393 _kdc_check_addresses(krb5_context context,
1394 krb5_kdc_configuration *config,
1395 HostAddresses *addresses, const struct sockaddr *from)
1397 krb5_error_code ret;
1398 krb5_address addr;
1399 krb5_boolean result;
1400 krb5_boolean only_netbios = TRUE;
1401 size_t i;
1403 if(config->check_ticket_addresses == 0)
1404 return TRUE;
1406 if(addresses == NULL)
1407 return config->allow_null_ticket_addresses;
1409 for (i = 0; i < addresses->len; ++i) {
1410 if (addresses->val[i].addr_type != KRB5_ADDRESS_NETBIOS) {
1411 only_netbios = FALSE;
1415 /* Windows sends it's netbios name, which I can only assume is
1416 * used for the 'allowed workstations' check. This is painful,
1417 * but we still want to check IP addresses if they happen to be
1418 * present.
1421 if(only_netbios)
1422 return config->allow_null_ticket_addresses;
1424 ret = krb5_sockaddr2address (context, from, &addr);
1425 if(ret)
1426 return FALSE;
1428 result = krb5_address_search(context, &addr, addresses);
1429 krb5_free_address (context, &addr);
1430 return result;
1437 static krb5_boolean
1438 send_pac_p(krb5_context context, KDC_REQ *req)
1440 krb5_error_code ret;
1441 PA_PAC_REQUEST pacreq;
1442 const PA_DATA *pa;
1443 int i = 0;
1445 pa = _kdc_find_padata(req, &i, KRB5_PADATA_PA_PAC_REQUEST);
1446 if (pa == NULL)
1447 return TRUE;
1449 ret = decode_PA_PAC_REQUEST(pa->padata_value.data,
1450 pa->padata_value.length,
1451 &pacreq,
1452 NULL);
1453 if (ret)
1454 return TRUE;
1455 i = pacreq.include_pac;
1456 free_PA_PAC_REQUEST(&pacreq);
1457 if (i == 0)
1458 return FALSE;
1459 return TRUE;
1466 static krb5_error_code
1467 generate_pac(kdc_request_t r, Key *skey)
1469 krb5_error_code ret;
1470 krb5_pac p = NULL;
1471 krb5_data data;
1473 ret = _kdc_pac_generate(r->context, r->client, &p);
1474 if (ret) {
1475 _kdc_r_log(r, 0, "PAC generation failed for -- %s",
1476 r->client_name);
1477 return ret;
1479 if (p == NULL)
1480 return 0;
1482 ret = _krb5_pac_sign(r->context, p, r->et.authtime,
1483 r->client->entry.principal,
1484 &skey->key, /* Server key */
1485 &skey->key, /* FIXME: should be krbtgt key */
1486 &data);
1487 krb5_pac_free(r->context, p);
1488 if (ret) {
1489 _kdc_r_log(r, 0, "PAC signing failed for -- %s",
1490 r->client_name);
1491 return ret;
1494 ret = _kdc_tkt_add_if_relevant_ad(r->context, &r->et,
1495 KRB5_AUTHDATA_WIN2K_PAC,
1496 &data);
1497 krb5_data_free(&data);
1499 return ret;
1506 krb5_boolean
1507 _kdc_is_anonymous(krb5_context context, krb5_principal principal)
1509 if (principal->name.name_type != KRB5_NT_WELLKNOWN ||
1510 principal->name.name_string.len != 2 ||
1511 strcmp(principal->name.name_string.val[0], KRB5_WELLKNOWN_NAME) != 0 ||
1512 strcmp(principal->name.name_string.val[1], KRB5_ANON_NAME) != 0)
1513 return 0;
1514 return 1;
1517 static int
1518 require_preauth_p(kdc_request_t r)
1520 return r->config->require_preauth
1521 || r->client->entry.flags.require_preauth
1522 || r->server->entry.flags.require_preauth;
1530 static krb5_error_code
1531 add_enc_pa_rep(kdc_request_t r)
1533 krb5_error_code ret;
1534 krb5_crypto crypto;
1535 Checksum checksum;
1536 krb5_data cdata;
1537 size_t len;
1539 ret = krb5_crypto_init(r->context, &r->reply_key, 0, &crypto);
1540 if (ret)
1541 return ret;
1543 ret = krb5_create_checksum(r->context, crypto,
1544 KRB5_KU_AS_REQ, 0,
1545 r->request.data, r->request.length,
1546 &checksum);
1547 krb5_crypto_destroy(r->context, crypto);
1548 if (ret)
1549 return ret;
1551 ASN1_MALLOC_ENCODE(Checksum, cdata.data, cdata.length,
1552 &checksum, &len, ret);
1553 free_Checksum(&checksum);
1554 if (ret)
1555 return ret;
1556 heim_assert(cdata.length == len, "ASN.1 internal error");
1558 if (r->ek.encrypted_pa_data == NULL) {
1559 ALLOC(r->ek.encrypted_pa_data);
1560 if (r->ek.encrypted_pa_data == NULL)
1561 return ENOMEM;
1563 ret = krb5_padata_add(r->context, r->ek.encrypted_pa_data,
1564 KRB5_PADATA_REQ_ENC_PA_REP, cdata.data, cdata.length);
1565 if (ret)
1566 return ret;
1568 return krb5_padata_add(r->context, r->ek.encrypted_pa_data,
1569 KRB5_PADATA_FX_FAST, NULL, 0);
1576 krb5_error_code
1577 _kdc_as_rep(kdc_request_t r,
1578 krb5_data *reply,
1579 const char *from,
1580 struct sockaddr *from_addr,
1581 int datagram_reply)
1583 krb5_context context = r->context;
1584 krb5_kdc_configuration *config = r->config;
1585 KDC_REQ *req = &r->req;
1586 KDC_REQ_BODY *b = NULL;
1587 AS_REP rep;
1588 KDCOptions f;
1589 krb5_enctype setype;
1590 krb5_error_code ret = 0;
1591 Key *skey;
1592 int found_pa = 0;
1593 int i, flags = HDB_F_FOR_AS_REQ;
1594 METHOD_DATA error_method;
1595 const PA_DATA *pa;
1597 memset(&rep, 0, sizeof(rep));
1598 error_method.len = 0;
1599 error_method.val = NULL;
1602 * Look for FAST armor and unwrap
1604 ret = _kdc_fast_unwrap_request(r);
1605 if (ret) {
1606 _kdc_r_log(r, 0, "FAST unwrap request from %s failed: %d", from, ret);
1607 goto out;
1610 b = &req->req_body;
1611 f = b->kdc_options;
1613 if (f.canonicalize)
1614 flags |= HDB_F_CANON;
1616 if(b->sname == NULL){
1617 ret = KRB5KRB_ERR_GENERIC;
1618 _kdc_set_e_text(r, "No server in request");
1619 } else{
1620 ret = _krb5_principalname2krb5_principal (context,
1621 &r->server_princ,
1622 *(b->sname),
1623 b->realm);
1624 if (ret == 0)
1625 ret = krb5_unparse_name(context, r->server_princ, &r->server_name);
1627 if (ret) {
1628 kdc_log(context, config, 0,
1629 "AS-REQ malformed server name from %s", from);
1630 goto out;
1632 if(b->cname == NULL){
1633 ret = KRB5KRB_ERR_GENERIC;
1634 _kdc_set_e_text(r, "No client in request");
1635 } else {
1636 ret = _krb5_principalname2krb5_principal (context,
1637 &r->client_princ,
1638 *(b->cname),
1639 b->realm);
1640 if (ret)
1641 goto out;
1643 ret = krb5_unparse_name(context, r->client_princ, &r->client_name);
1645 if (ret) {
1646 kdc_log(context, config, 0,
1647 "AS-REQ malformed client name from %s", from);
1648 goto out;
1651 kdc_log(context, config, 0, "AS-REQ %s from %s for %s",
1652 r->client_name, from, r->server_name);
1658 if (_kdc_is_anonymous(context, r->client_princ)) {
1659 if (!b->kdc_options.request_anonymous) {
1660 kdc_log(context, config, 0, "Anonymous ticket w/o anonymous flag");
1661 ret = KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN;
1662 goto out;
1664 } else if (b->kdc_options.request_anonymous) {
1665 kdc_log(context, config, 0,
1666 "Request for a anonymous ticket with non "
1667 "anonymous client name: %s", r->client_name);
1668 ret = KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN;
1669 goto out;
1676 ret = _kdc_db_fetch(context, config, r->client_princ,
1677 HDB_F_GET_CLIENT | flags, NULL,
1678 &r->clientdb, &r->client);
1679 if(ret == HDB_ERR_NOT_FOUND_HERE) {
1680 kdc_log(context, config, 5, "client %s does not have secrets at this KDC, need to proxy",
1681 r->client_name);
1682 goto out;
1683 } else if(ret){
1684 const char *msg = krb5_get_error_message(context, ret);
1685 kdc_log(context, config, 0, "UNKNOWN -- %s: %s", r->client_name, msg);
1686 krb5_free_error_message(context, msg);
1687 ret = KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN;
1688 goto out;
1690 ret = _kdc_db_fetch(context, config, r->server_princ,
1691 HDB_F_GET_SERVER|HDB_F_GET_KRBTGT | flags,
1692 NULL, NULL, &r->server);
1693 if(ret == HDB_ERR_NOT_FOUND_HERE) {
1694 kdc_log(context, config, 5, "target %s does not have secrets at this KDC, need to proxy",
1695 r->server_name);
1696 goto out;
1697 } else if(ret){
1698 const char *msg = krb5_get_error_message(context, ret);
1699 kdc_log(context, config, 0, "UNKNOWN -- %s: %s", r->server_name, msg);
1700 krb5_free_error_message(context, msg);
1701 ret = KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN;
1702 goto out;
1706 * Select a session enctype from the list of the crypto system
1707 * supported enctypes that is supported by the client and is one of
1708 * the enctype of the enctype of the service (likely krbtgt).
1710 * The latter is used as a hint of what enctypes all KDC support,
1711 * to make sure a newer version of KDC won't generate a session
1712 * enctype that an older version of a KDC in the same realm can't
1713 * decrypt.
1716 ret = _kdc_find_etype(context,
1717 krb5_principal_is_krbtgt(context, r->server_princ) ?
1718 config->tgt_use_strongest_session_key :
1719 config->svc_use_strongest_session_key, FALSE,
1720 r->client, b->etype.val, b->etype.len, &r->sessionetype,
1721 NULL);
1722 if (ret) {
1723 kdc_log(context, config, 0,
1724 "Client (%s) from %s has no common enctypes with KDC "
1725 "to use for the session key",
1726 r->client_name, from);
1727 goto out;
1731 * Pre-auth processing
1734 if(req->padata){
1735 unsigned int n;
1737 log_patypes(context, config, req->padata);
1739 /* Check if preauth matching */
1741 for (n = 0; !found_pa && n < sizeof(pat) / sizeof(pat[0]); n++) {
1742 if (pat[n].validate == NULL)
1743 continue;
1744 if (r->armor_crypto == NULL && (pat[n].flags & PA_REQ_FAST))
1745 continue;
1747 kdc_log(context, config, 5,
1748 "Looking for %s pa-data -- %s", pat[n].name, r->client_name);
1749 i = 0;
1750 pa = _kdc_find_padata(req, &i, pat[n].type);
1751 if (pa) {
1752 ret = pat[n].validate(r, pa);
1753 if (ret == 0) {
1754 kdc_log(context, config, 0,
1755 "%s pre-authentication succeeded -- %s",
1756 pat[n].name, r->client_name);
1757 found_pa = 1;
1758 r->et.flags.pre_authent = 1;
1764 if (found_pa == 0) {
1765 Key *ckey = NULL;
1766 size_t n;
1768 for (n = 0; n < sizeof(pat) / sizeof(pat[0]); n++) {
1769 if ((pat[n].flags & PA_ANNOUNCE) == 0)
1770 continue;
1771 ret = krb5_padata_add(context, &error_method,
1772 pat[n].type, NULL, 0);
1773 if (ret)
1774 goto out;
1778 * If there is a client key, send ETYPE_INFO{,2}
1780 ret = _kdc_find_etype(context,
1781 config->preauth_use_strongest_session_key, TRUE,
1782 r->client, b->etype.val, b->etype.len, NULL, &ckey);
1783 if (ret == 0) {
1786 * RFC4120 requires:
1787 * - If the client only knows about old enctypes, then send
1788 * both info replies (we send 'info' first in the list).
1789 * - If the client is 'modern', because it knows about 'new'
1790 * enctype types, then only send the 'info2' reply.
1792 * Before we send the full list of etype-info data, we pick
1793 * the client key we would have used anyway below, just pick
1794 * that instead.
1797 if (older_enctype(ckey->key.keytype)) {
1798 ret = get_pa_etype_info(context, config,
1799 &error_method, ckey);
1800 if (ret)
1801 goto out;
1803 ret = get_pa_etype_info2(context, config,
1804 &error_method, ckey);
1805 if (ret)
1806 goto out;
1810 * send requre preauth is its required or anon is requested,
1811 * anon is today only allowed via preauth mechanisms.
1813 if (require_preauth_p(r) || b->kdc_options.request_anonymous) {
1814 ret = KRB5KDC_ERR_PREAUTH_REQUIRED;
1815 _kdc_set_e_text(r, "Need to use PA-ENC-TIMESTAMP/PA-PK-AS-REQ");
1816 goto out;
1819 if (ckey == NULL) {
1820 ret = KRB5KDC_ERR_CLIENT_NOTYET;
1821 _kdc_set_e_text(r, "Doesn't have a client key available");
1822 goto out;
1824 krb5_free_keyblock_contents(r->context, &r->reply_key);
1825 ret = krb5_copy_keyblock_contents(r->context, &ckey->key, &r->reply_key);
1826 if (ret)
1827 goto out;
1830 if (r->clientdb->hdb_auth_status)
1831 r->clientdb->hdb_auth_status(context, r->clientdb, r->client,
1832 HDB_AUTH_SUCCESS);
1835 * Verify flags after the user been required to prove its identity
1836 * with in a preauth mech.
1839 ret = _kdc_check_access(context, config, r->client, r->client_name,
1840 r->server, r->server_name,
1841 req, &error_method);
1842 if(ret)
1843 goto out;
1846 * Select the best encryption type for the KDC with out regard to
1847 * the client since the client never needs to read that data.
1850 ret = _kdc_get_preferred_key(context, config,
1851 r->server, r->server_name,
1852 &setype, &skey);
1853 if(ret)
1854 goto out;
1856 if(f.renew || f.validate || f.proxy || f.forwarded || f.enc_tkt_in_skey
1857 || (f.request_anonymous && !config->allow_anonymous)) {
1858 ret = KRB5KDC_ERR_BADOPTION;
1859 _kdc_set_e_text(r, "Bad KDC options");
1860 goto out;
1864 * Build reply
1867 rep.pvno = 5;
1868 rep.msg_type = krb_as_rep;
1870 ret = copy_Realm(&r->client->entry.principal->realm, &rep.crealm);
1871 if (ret)
1872 goto out;
1873 ret = _krb5_principal2principalname(&rep.cname, r->client->entry.principal);
1874 if (ret)
1875 goto out;
1877 rep.ticket.tkt_vno = 5;
1878 copy_Realm(&r->server->entry.principal->realm, &rep.ticket.realm);
1879 _krb5_principal2principalname(&rep.ticket.sname,
1880 r->server->entry.principal);
1881 /* java 1.6 expects the name to be the same type, lets allow that
1882 * uncomplicated name-types. */
1883 #define CNT(sp,t) (((sp)->sname->name_type) == KRB5_NT_##t)
1884 if (CNT(b, UNKNOWN) || CNT(b, PRINCIPAL) || CNT(b, SRV_INST) || CNT(b, SRV_HST) || CNT(b, SRV_XHST))
1885 rep.ticket.sname.name_type = b->sname->name_type;
1886 #undef CNT
1888 r->et.flags.initial = 1;
1889 if(r->client->entry.flags.forwardable && r->server->entry.flags.forwardable)
1890 r->et.flags.forwardable = f.forwardable;
1891 else if (f.forwardable) {
1892 _kdc_set_e_text(r, "Ticket may not be forwardable");
1893 ret = KRB5KDC_ERR_POLICY;
1894 goto out;
1896 if(r->client->entry.flags.proxiable && r->server->entry.flags.proxiable)
1897 r->et.flags.proxiable = f.proxiable;
1898 else if (f.proxiable) {
1899 _kdc_set_e_text(r, "Ticket may not be proxiable");
1900 ret = KRB5KDC_ERR_POLICY;
1901 goto out;
1903 if(r->client->entry.flags.postdate && r->server->entry.flags.postdate)
1904 r->et.flags.may_postdate = f.allow_postdate;
1905 else if (f.allow_postdate){
1906 _kdc_set_e_text(r, "Ticket may not be postdate");
1907 ret = KRB5KDC_ERR_POLICY;
1908 goto out;
1911 /* check for valid set of addresses */
1912 if(!_kdc_check_addresses(context, config, b->addresses, from_addr)) {
1913 _kdc_set_e_text(r, "Bad address list in requested");
1914 ret = KRB5KRB_AP_ERR_BADADDR;
1915 goto out;
1918 ret = copy_PrincipalName(&rep.cname, &r->et.cname);
1919 if (ret)
1920 goto out;
1921 ret = copy_Realm(&rep.crealm, &r->et.crealm);
1922 if (ret)
1923 goto out;
1926 time_t start;
1927 time_t t;
1929 start = r->et.authtime = kdc_time;
1931 if(f.postdated && req->req_body.from){
1932 ALLOC(r->et.starttime);
1933 start = *r->et.starttime = *req->req_body.from;
1934 r->et.flags.invalid = 1;
1935 r->et.flags.postdated = 1; /* XXX ??? */
1937 _kdc_fix_time(&b->till);
1938 t = *b->till;
1940 /* be careful not overflowing */
1942 if(r->client->entry.max_life)
1943 t = start + min(t - start, *r->client->entry.max_life);
1944 if(r->server->entry.max_life)
1945 t = start + min(t - start, *r->server->entry.max_life);
1946 #if 0
1947 t = min(t, start + realm->max_life);
1948 #endif
1949 r->et.endtime = t;
1950 if(f.renewable_ok && r->et.endtime < *b->till){
1951 f.renewable = 1;
1952 if(b->rtime == NULL){
1953 ALLOC(b->rtime);
1954 *b->rtime = 0;
1956 if(*b->rtime < *b->till)
1957 *b->rtime = *b->till;
1959 if(f.renewable && b->rtime){
1960 t = *b->rtime;
1961 if(t == 0)
1962 t = MAX_TIME;
1963 if(r->client->entry.max_renew)
1964 t = start + min(t - start, *r->client->entry.max_renew);
1965 if(r->server->entry.max_renew)
1966 t = start + min(t - start, *r->server->entry.max_renew);
1967 #if 0
1968 t = min(t, start + realm->max_renew);
1969 #endif
1970 ALLOC(r->et.renew_till);
1971 *r->et.renew_till = t;
1972 r->et.flags.renewable = 1;
1976 if (f.request_anonymous)
1977 r->et.flags.anonymous = 1;
1979 if(b->addresses){
1980 ALLOC(r->et.caddr);
1981 copy_HostAddresses(b->addresses, r->et.caddr);
1984 r->et.transited.tr_type = DOMAIN_X500_COMPRESS;
1985 krb5_data_zero(&r->et.transited.contents);
1987 /* The MIT ASN.1 library (obviously) doesn't tell lengths encoded
1988 * as 0 and as 0x80 (meaning indefinite length) apart, and is thus
1989 * incapable of correctly decoding SEQUENCE OF's of zero length.
1991 * To fix this, always send at least one no-op last_req
1993 * If there's a pw_end or valid_end we will use that,
1994 * otherwise just a dummy lr.
1996 r->ek.last_req.val = malloc(2 * sizeof(*r->ek.last_req.val));
1997 if (r->ek.last_req.val == NULL) {
1998 ret = ENOMEM;
1999 goto out;
2001 r->ek.last_req.len = 0;
2002 if (r->client->entry.pw_end
2003 && (config->kdc_warn_pwexpire == 0
2004 || kdc_time + config->kdc_warn_pwexpire >= *r->client->entry.pw_end)) {
2005 r->ek.last_req.val[r->ek.last_req.len].lr_type = LR_PW_EXPTIME;
2006 r->ek.last_req.val[r->ek.last_req.len].lr_value = *r->client->entry.pw_end;
2007 ++r->ek.last_req.len;
2009 if (r->client->entry.valid_end) {
2010 r->ek.last_req.val[r->ek.last_req.len].lr_type = LR_ACCT_EXPTIME;
2011 r->ek.last_req.val[r->ek.last_req.len].lr_value = *r->client->entry.valid_end;
2012 ++r->ek.last_req.len;
2014 if (r->ek.last_req.len == 0) {
2015 r->ek.last_req.val[r->ek.last_req.len].lr_type = LR_NONE;
2016 r->ek.last_req.val[r->ek.last_req.len].lr_value = 0;
2017 ++r->ek.last_req.len;
2019 r->ek.nonce = b->nonce;
2020 if (r->client->entry.valid_end || r->client->entry.pw_end) {
2021 ALLOC(r->ek.key_expiration);
2022 if (r->client->entry.valid_end) {
2023 if (r->client->entry.pw_end)
2024 *r->ek.key_expiration = min(*r->client->entry.valid_end,
2025 *r->client->entry.pw_end);
2026 else
2027 *r->ek.key_expiration = *r->client->entry.valid_end;
2028 } else
2029 *r->ek.key_expiration = *r->client->entry.pw_end;
2030 } else
2031 r->ek.key_expiration = NULL;
2032 r->ek.flags = r->et.flags;
2033 r->ek.authtime = r->et.authtime;
2034 if (r->et.starttime) {
2035 ALLOC(r->ek.starttime);
2036 *r->ek.starttime = *r->et.starttime;
2038 r->ek.endtime = r->et.endtime;
2039 if (r->et.renew_till) {
2040 ALLOC(r->ek.renew_till);
2041 *r->ek.renew_till = *r->et.renew_till;
2043 copy_Realm(&rep.ticket.realm, &r->ek.srealm);
2044 copy_PrincipalName(&rep.ticket.sname, &r->ek.sname);
2045 if(r->et.caddr){
2046 ALLOC(r->ek.caddr);
2047 copy_HostAddresses(r->et.caddr, r->ek.caddr);
2051 * Check and session and reply keys
2054 if (r->session_key.keytype == ETYPE_NULL) {
2055 ret = krb5_generate_random_keyblock(context, r->sessionetype, &r->session_key);
2056 if (ret)
2057 goto out;
2060 if (r->reply_key.keytype == ETYPE_NULL) {
2061 _kdc_set_e_text(r, "Client have no reply key");
2062 ret = KRB5KDC_ERR_CLIENT_NOTYET;
2063 goto out;
2066 ret = copy_EncryptionKey(&r->session_key, &r->et.key);
2067 if (ret)
2068 goto out;
2070 ret = copy_EncryptionKey(&r->session_key, &r->ek.key);
2071 if (ret)
2072 goto out;
2075 * Add signing of alias referral
2078 if (f.canonicalize) {
2079 PA_ClientCanonicalized canon;
2080 krb5_data data;
2081 PA_DATA tmppa;
2082 krb5_crypto cryptox;
2083 size_t len = 0;
2085 memset(&canon, 0, sizeof(canon));
2087 canon.names.requested_name = *b->cname;
2088 canon.names.mapped_name = r->client->entry.principal->name;
2090 ASN1_MALLOC_ENCODE(PA_ClientCanonicalizedNames, data.data, data.length,
2091 &canon.names, &len, ret);
2092 if (ret)
2093 goto out;
2094 if (data.length != len)
2095 krb5_abortx(context, "internal asn.1 error");
2097 /* sign using "returned session key" */
2098 ret = krb5_crypto_init(context, &r->et.key, 0, &cryptox);
2099 if (ret) {
2100 free(data.data);
2101 goto out;
2104 ret = krb5_create_checksum(context, cryptox,
2105 KRB5_KU_CANONICALIZED_NAMES, 0,
2106 data.data, data.length,
2107 &canon.canon_checksum);
2108 free(data.data);
2109 krb5_crypto_destroy(context, cryptox);
2110 if (ret)
2111 goto out;
2113 ASN1_MALLOC_ENCODE(PA_ClientCanonicalized, data.data, data.length,
2114 &canon, &len, ret);
2115 free_Checksum(&canon.canon_checksum);
2116 if (ret)
2117 goto out;
2118 if (data.length != len)
2119 krb5_abortx(context, "internal asn.1 error");
2121 tmppa.padata_type = KRB5_PADATA_CLIENT_CANONICALIZED;
2122 tmppa.padata_value = data;
2123 ret = add_METHOD_DATA(&r->outpadata, &tmppa);
2124 free(data.data);
2125 if (ret)
2126 goto out;
2129 if (r->outpadata.len) {
2131 ALLOC(rep.padata);
2132 if (rep.padata == NULL) {
2133 ret = ENOMEM;
2134 goto out;
2136 ret = copy_METHOD_DATA(&r->outpadata, rep.padata);
2137 if (ret)
2138 goto out;
2141 /* Add the PAC */
2142 if (send_pac_p(context, req)) {
2143 generate_pac(r, skey);
2146 _kdc_log_timestamp(context, config, "AS-REQ", r->et.authtime, r->et.starttime,
2147 r->et.endtime, r->et.renew_till);
2149 /* do this as the last thing since this signs the EncTicketPart */
2150 ret = _kdc_add_KRB5SignedPath(context,
2151 config,
2152 r->server,
2153 setype,
2154 r->client->entry.principal,
2155 NULL,
2156 NULL,
2157 &r->et);
2158 if (ret)
2159 goto out;
2161 log_as_req(context, config, r->reply_key.keytype, setype, b);
2164 * We always say we support FAST/enc-pa-rep
2167 r->et.flags.enc_pa_rep = r->ek.flags.enc_pa_rep = 1;
2170 * Add REQ_ENC_PA_REP if client supports it
2173 i = 0;
2174 pa = _kdc_find_padata(req, &i, KRB5_PADATA_REQ_ENC_PA_REP);
2175 if (pa) {
2177 ret = add_enc_pa_rep(r);
2178 if (ret) {
2179 const char *msg = krb5_get_error_message(r->context, ret);
2180 _kdc_r_log(r, 0, "add_enc_pa_rep failed: %s: %d", msg, ret);
2181 krb5_free_error_message(r->context, msg);
2182 goto out;
2190 ret = _kdc_encode_reply(context, config,
2191 r->armor_crypto, req->req_body.nonce,
2192 &rep, &r->et, &r->ek, setype, r->server->entry.kvno,
2193 &skey->key, r->client->entry.kvno,
2194 &r->reply_key, 0, &r->e_text, reply);
2195 if (ret)
2196 goto out;
2199 * Check if message too large
2201 if (datagram_reply && reply->length > config->max_datagram_reply_length) {
2202 krb5_data_free(reply);
2203 ret = KRB5KRB_ERR_RESPONSE_TOO_BIG;
2204 _kdc_set_e_text(r, "Reply packet too large");
2207 out:
2208 free_AS_REP(&rep);
2211 * In case of a non proxy error, build an error message.
2213 if(ret != 0 && ret != HDB_ERR_NOT_FOUND_HERE) {
2214 ret = _kdc_fast_mk_error(context, r,
2215 &error_method,
2216 r->armor_crypto,
2217 &req->req_body,
2218 ret, r->e_text,
2219 r->client_princ, r->server_princ,
2220 NULL, NULL,
2221 reply);
2222 if (ret)
2223 goto out2;
2225 out2:
2226 free_EncTicketPart(&r->et);
2227 free_EncKDCRepPart(&r->ek);
2228 free_KDCFastState(&r->fast);
2230 if (error_method.len)
2231 free_METHOD_DATA(&error_method);
2232 if (r->outpadata.len)
2233 free_METHOD_DATA(&r->outpadata);
2234 if (r->client_princ) {
2235 krb5_free_principal(context, r->client_princ);
2236 r->client_princ = NULL;
2238 if (r->client_name) {
2239 free(r->client_name);
2240 r->client_name = NULL;
2242 if (r->server_princ){
2243 krb5_free_principal(context, r->server_princ);
2244 r->server_princ = NULL;
2246 if (r->server_name) {
2247 free(r->server_name);
2248 r->server_name = NULL;
2250 if (r->client)
2251 _kdc_free_ent(context, r->client);
2252 if (r->server)
2253 _kdc_free_ent(context, r->server);
2254 if (r->armor_crypto) {
2255 krb5_crypto_destroy(r->context, r->armor_crypto);
2256 r->armor_crypto = NULL;
2258 krb5_free_keyblock_contents(r->context, &r->reply_key);
2259 krb5_free_keyblock_contents(r->context, &r->session_key);
2260 return ret;
2264 * Add the AuthorizationData `data´ of `type´ to the last element in
2265 * the sequence of authorization_data in `tkt´ wrapped in an IF_RELEVANT
2268 krb5_error_code
2269 _kdc_tkt_add_if_relevant_ad(krb5_context context,
2270 EncTicketPart *tkt,
2271 int type,
2272 const krb5_data *data)
2274 krb5_error_code ret;
2275 size_t size = 0;
2277 if (tkt->authorization_data == NULL) {
2278 tkt->authorization_data = calloc(1, sizeof(*tkt->authorization_data));
2279 if (tkt->authorization_data == NULL) {
2280 krb5_set_error_message(context, ENOMEM, "out of memory");
2281 return ENOMEM;
2285 /* add the entry to the last element */
2287 AuthorizationData ad = { 0, NULL };
2288 AuthorizationDataElement ade;
2290 ade.ad_type = type;
2291 ade.ad_data = *data;
2293 ret = add_AuthorizationData(&ad, &ade);
2294 if (ret) {
2295 krb5_set_error_message(context, ret, "add AuthorizationData failed");
2296 return ret;
2299 ade.ad_type = KRB5_AUTHDATA_IF_RELEVANT;
2301 ASN1_MALLOC_ENCODE(AuthorizationData,
2302 ade.ad_data.data, ade.ad_data.length,
2303 &ad, &size, ret);
2304 free_AuthorizationData(&ad);
2305 if (ret) {
2306 krb5_set_error_message(context, ret, "ASN.1 encode of "
2307 "AuthorizationData failed");
2308 return ret;
2310 if (ade.ad_data.length != size)
2311 krb5_abortx(context, "internal asn.1 encoder error");
2313 ret = add_AuthorizationData(tkt->authorization_data, &ade);
2314 der_free_octet_string(&ade.ad_data);
2315 if (ret) {
2316 krb5_set_error_message(context, ret, "add AuthorizationData failed");
2317 return ret;
2321 return 0;