xref: /freebsd/crypto/heimdal/lib/krb5/get_in_tkt.c (revision eacee0ff7ec955b32e09515246bd97b6edcd2b0f)
1 /*
2  * Copyright (c) 1997 - 2001 Kungliga Tekniska H�gskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
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.
16  *
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.
20  *
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.
32  */
33 
34 #include "krb5_locl.h"
35 
36 RCSID("$Id: get_in_tkt.c,v 1.103 2002/01/06 23:10:06 assar Exp $");
37 
38 krb5_error_code
39 krb5_init_etype (krb5_context context,
40 		 unsigned *len,
41 		 krb5_enctype **val,
42 		 const krb5_enctype *etypes)
43 {
44     int i;
45     krb5_error_code ret;
46     krb5_enctype *tmp;
47 
48     ret = 0;
49     if (etypes)
50 	tmp = (krb5_enctype*)etypes;
51     else {
52 	ret = krb5_get_default_in_tkt_etypes(context,
53 					     &tmp);
54 	if (ret)
55 	    return ret;
56     }
57 
58     for (i = 0; tmp[i]; ++i)
59 	;
60     *len = i;
61     *val = malloc(i * sizeof(**val));
62     if (i != 0 && *val == NULL) {
63 	ret = ENOMEM;
64 	krb5_set_error_string(context, "malloc: out of memory");
65 	goto cleanup;
66     }
67     memmove (*val,
68 	     tmp,
69 	     i * sizeof(*tmp));
70 cleanup:
71     if (etypes == NULL)
72 	free (tmp);
73     return ret;
74 }
75 
76 
77 static krb5_error_code
78 decrypt_tkt (krb5_context context,
79 	     krb5_keyblock *key,
80 	     krb5_key_usage usage,
81 	     krb5_const_pointer decrypt_arg,
82 	     krb5_kdc_rep *dec_rep)
83 {
84     krb5_error_code ret;
85     krb5_data data;
86     size_t size;
87     krb5_crypto crypto;
88 
89     ret = krb5_crypto_init(context, key, 0, &crypto);
90     if (ret)
91 	return ret;
92 
93     ret = krb5_decrypt_EncryptedData (context,
94 				      crypto,
95 				      usage,
96 				      &dec_rep->kdc_rep.enc_part,
97 				      &data);
98     krb5_crypto_destroy(context, crypto);
99 
100     if (ret)
101 	return ret;
102 
103     ret = krb5_decode_EncASRepPart(context,
104 				   data.data,
105 				   data.length,
106 				   &dec_rep->enc_part,
107 				   &size);
108     if (ret)
109 	ret = krb5_decode_EncTGSRepPart(context,
110 					data.data,
111 					data.length,
112 					&dec_rep->enc_part,
113 					&size);
114     krb5_data_free (&data);
115     if (ret)
116 	return ret;
117     return 0;
118 }
119 
120 int
121 _krb5_extract_ticket(krb5_context context,
122 		     krb5_kdc_rep *rep,
123 		     krb5_creds *creds,
124 		     krb5_keyblock *key,
125 		     krb5_const_pointer keyseed,
126 		     krb5_key_usage key_usage,
127 		     krb5_addresses *addrs,
128 		     unsigned nonce,
129 		     krb5_boolean allow_server_mismatch,
130 		     krb5_boolean ignore_cname,
131 		     krb5_decrypt_proc decrypt_proc,
132 		     krb5_const_pointer decryptarg)
133 {
134     krb5_error_code ret;
135     krb5_principal tmp_principal;
136     int tmp;
137     time_t tmp_time;
138     krb5_timestamp sec_now;
139 
140     ret = principalname2krb5_principal (&tmp_principal,
141 					rep->kdc_rep.cname,
142 					rep->kdc_rep.crealm);
143     if (ret)
144 	goto out;
145 
146     /* compare client */
147 
148     if (!ignore_cname) {
149 	tmp = krb5_principal_compare (context, tmp_principal, creds->client);
150 	if (!tmp) {
151 	    krb5_free_principal (context, tmp_principal);
152 	    krb5_clear_error_string (context);
153 	    ret = KRB5KRB_AP_ERR_MODIFIED;
154 	    goto out;
155 	}
156     }
157 
158     krb5_free_principal (context, creds->client);
159     creds->client = tmp_principal;
160 
161     /* extract ticket */
162     {
163 	unsigned char *buf;
164 	size_t len;
165 	len = length_Ticket(&rep->kdc_rep.ticket);
166 	buf = malloc(len);
167 	if(buf == NULL) {
168 	    krb5_set_error_string(context, "malloc: out of memory");
169 	    ret = ENOMEM;
170 	    goto out;
171 	}
172 	encode_Ticket(buf + len - 1, len, &rep->kdc_rep.ticket, &len);
173 	creds->ticket.data = buf;
174 	creds->ticket.length = len;
175 	creds->second_ticket.length = 0;
176 	creds->second_ticket.data   = NULL;
177     }
178 
179     /* compare server */
180 
181     ret = principalname2krb5_principal (&tmp_principal,
182 					rep->kdc_rep.ticket.sname,
183 					rep->kdc_rep.ticket.realm);
184     if (ret)
185 	goto out;
186     if(allow_server_mismatch){
187 	krb5_free_principal(context, creds->server);
188 	creds->server = tmp_principal;
189 	tmp_principal = NULL;
190     }else{
191 	tmp = krb5_principal_compare (context, tmp_principal, creds->server);
192 	krb5_free_principal (context, tmp_principal);
193 	if (!tmp) {
194 	    ret = KRB5KRB_AP_ERR_MODIFIED;
195 	    krb5_clear_error_string (context);
196 	    goto out;
197 	}
198     }
199 
200     /* decrypt */
201 
202     if (decrypt_proc == NULL)
203 	decrypt_proc = decrypt_tkt;
204 
205     ret = (*decrypt_proc)(context, key, key_usage, decryptarg, rep);
206     if (ret)
207 	goto out;
208 
209 #if 0
210     /* XXX should this decode be here, or in the decrypt_proc? */
211     ret = krb5_decode_keyblock(context, &rep->enc_part.key, 1);
212     if(ret)
213 	goto out;
214 #endif
215 
216     /* compare nonces */
217 
218     if (nonce != rep->enc_part.nonce) {
219 	ret = KRB5KRB_AP_ERR_MODIFIED;
220 	krb5_set_error_string(context, "malloc: out of memory");
221 	goto out;
222     }
223 
224     /* set kdc-offset */
225 
226     krb5_timeofday (context, &sec_now);
227     if (context->kdc_sec_offset == 0
228 	&& krb5_config_get_bool (context, NULL,
229 				 "libdefaults",
230 				 "kdc_timesync",
231 				 NULL)) {
232 	context->kdc_sec_offset = rep->enc_part.authtime - sec_now;
233 	krb5_timeofday (context, &sec_now);
234     }
235 
236     /* check all times */
237 
238     if (rep->enc_part.starttime) {
239 	tmp_time = *rep->enc_part.starttime;
240     } else
241 	tmp_time = rep->enc_part.authtime;
242 
243     if (creds->times.starttime == 0
244 	&& abs(tmp_time - sec_now) > context->max_skew) {
245 	ret = KRB5KRB_AP_ERR_SKEW;
246 	krb5_set_error_string (context,
247 			       "time skew (%d) larger than max (%d)",
248 			       abs(tmp_time - sec_now),
249 			       (int)context->max_skew);
250 	goto out;
251     }
252 
253     if (creds->times.starttime != 0
254 	&& tmp_time != creds->times.starttime) {
255 	krb5_clear_error_string (context);
256 	ret = KRB5KRB_AP_ERR_MODIFIED;
257 	goto out;
258     }
259 
260     creds->times.starttime = tmp_time;
261 
262     if (rep->enc_part.renew_till) {
263 	tmp_time = *rep->enc_part.renew_till;
264     } else
265 	tmp_time = 0;
266 
267     if (creds->times.renew_till != 0
268 	&& tmp_time > creds->times.renew_till) {
269 	krb5_clear_error_string (context);
270 	ret = KRB5KRB_AP_ERR_MODIFIED;
271 	goto out;
272     }
273 
274     creds->times.renew_till = tmp_time;
275 
276     creds->times.authtime = rep->enc_part.authtime;
277 
278     if (creds->times.endtime != 0
279 	&& rep->enc_part.endtime > creds->times.endtime) {
280 	krb5_clear_error_string (context);
281 	ret = KRB5KRB_AP_ERR_MODIFIED;
282 	goto out;
283     }
284 
285     creds->times.endtime  = rep->enc_part.endtime;
286 
287     if(rep->enc_part.caddr)
288 	krb5_copy_addresses (context, rep->enc_part.caddr, &creds->addresses);
289     else if(addrs)
290 	krb5_copy_addresses (context, addrs, &creds->addresses);
291     else {
292 	creds->addresses.len = 0;
293 	creds->addresses.val = NULL;
294     }
295     creds->flags.b = rep->enc_part.flags;
296 
297     creds->authdata.len = 0;
298     creds->authdata.val = NULL;
299     creds->session.keyvalue.length = 0;
300     creds->session.keyvalue.data   = NULL;
301     creds->session.keytype = rep->enc_part.key.keytype;
302     ret = krb5_data_copy (&creds->session.keyvalue,
303 			  rep->enc_part.key.keyvalue.data,
304 			  rep->enc_part.key.keyvalue.length);
305 
306 out:
307     memset (rep->enc_part.key.keyvalue.data, 0,
308 	    rep->enc_part.key.keyvalue.length);
309     return ret;
310 }
311 
312 
313 static krb5_error_code
314 make_pa_enc_timestamp(krb5_context context, PA_DATA *pa,
315 		      krb5_enctype etype, krb5_keyblock *key)
316 {
317     PA_ENC_TS_ENC p;
318     u_char buf[1024];
319     size_t len;
320     EncryptedData encdata;
321     krb5_error_code ret;
322     int32_t sec, usec;
323     int usec2;
324     krb5_crypto crypto;
325 
326     krb5_us_timeofday (context, &sec, &usec);
327     p.patimestamp = sec;
328     usec2         = usec;
329     p.pausec      = &usec2;
330 
331     ret = encode_PA_ENC_TS_ENC(buf + sizeof(buf) - 1,
332 			       sizeof(buf),
333 			       &p,
334 			       &len);
335     if (ret)
336 	return ret;
337 
338     ret = krb5_crypto_init(context, key, 0, &crypto);
339     if (ret)
340 	return ret;
341     ret = krb5_encrypt_EncryptedData(context,
342 				     crypto,
343 				     KRB5_KU_PA_ENC_TIMESTAMP,
344 				     buf + sizeof(buf) - len,
345 				     len,
346 				     0,
347 				     &encdata);
348     krb5_crypto_destroy(context, crypto);
349     if (ret)
350 	return ret;
351 
352     ret = encode_EncryptedData(buf + sizeof(buf) - 1,
353 			       sizeof(buf),
354 			       &encdata,
355 			       &len);
356     free_EncryptedData(&encdata);
357     if (ret)
358 	return ret;
359     pa->padata_type = KRB5_PADATA_ENC_TIMESTAMP;
360     pa->padata_value.length = 0;
361     krb5_data_copy(&pa->padata_value,
362 		   buf + sizeof(buf) - len,
363 		   len);
364     return 0;
365 }
366 
367 static krb5_error_code
368 add_padata(krb5_context context,
369 	   METHOD_DATA *md,
370 	   krb5_principal client,
371 	   krb5_key_proc key_proc,
372 	   krb5_const_pointer keyseed,
373 	   krb5_enctype *enctypes,
374 	   unsigned netypes,
375 	   krb5_salt *salt)
376 {
377     krb5_error_code ret;
378     PA_DATA *pa2;
379     krb5_salt salt2;
380     krb5_enctype *ep;
381     int i;
382 
383     if(salt == NULL) {
384 	/* default to standard salt */
385 	ret = krb5_get_pw_salt (context, client, &salt2);
386 	salt = &salt2;
387     }
388     if (!enctypes) {
389 	enctypes = context->etypes;
390 	netypes = 0;
391 	for (ep = enctypes; *ep != ETYPE_NULL; ep++)
392 	    netypes++;
393     }
394     pa2 = realloc (md->val, (md->len + netypes) * sizeof(*md->val));
395     if (pa2 == NULL) {
396 	krb5_set_error_string(context, "malloc: out of memory");
397 	return ENOMEM;
398     }
399     md->val = pa2;
400 
401     for (i = 0; i < netypes; ++i) {
402 	krb5_keyblock *key;
403 
404 	ret = (*key_proc)(context, enctypes[i], *salt, keyseed, &key);
405 	if (ret)
406 	    continue;
407 	ret = make_pa_enc_timestamp (context, &md->val[md->len],
408 				     enctypes[i], key);
409 	krb5_free_keyblock (context, key);
410 	if (ret)
411 	    return ret;
412 	++md->len;
413     }
414     if(salt == &salt2)
415 	krb5_free_salt(context, salt2);
416     return 0;
417 }
418 
419 static krb5_error_code
420 init_as_req (krb5_context context,
421 	     krb5_kdc_flags opts,
422 	     krb5_creds *creds,
423 	     const krb5_addresses *addrs,
424 	     const krb5_enctype *etypes,
425 	     const krb5_preauthtype *ptypes,
426 	     const krb5_preauthdata *preauth,
427 	     krb5_key_proc key_proc,
428 	     krb5_const_pointer keyseed,
429 	     unsigned nonce,
430 	     AS_REQ *a)
431 {
432     krb5_error_code ret;
433     krb5_salt salt;
434 
435     memset(a, 0, sizeof(*a));
436 
437     a->pvno = 5;
438     a->msg_type = krb_as_req;
439     a->req_body.kdc_options = opts.b;
440     a->req_body.cname = malloc(sizeof(*a->req_body.cname));
441     if (a->req_body.cname == NULL) {
442 	ret = ENOMEM;
443 	krb5_set_error_string(context, "malloc: out of memory");
444 	goto fail;
445     }
446     a->req_body.sname = malloc(sizeof(*a->req_body.sname));
447     if (a->req_body.sname == NULL) {
448 	ret = ENOMEM;
449 	krb5_set_error_string(context, "malloc: out of memory");
450 	goto fail;
451     }
452     ret = krb5_principal2principalname (a->req_body.cname, creds->client);
453     if (ret)
454 	goto fail;
455     ret = krb5_principal2principalname (a->req_body.sname, creds->server);
456     if (ret)
457 	goto fail;
458     ret = copy_Realm(&creds->client->realm, &a->req_body.realm);
459     if (ret)
460 	goto fail;
461 
462     if(creds->times.starttime) {
463 	a->req_body.from = malloc(sizeof(*a->req_body.from));
464 	if (a->req_body.from == NULL) {
465 	    ret = ENOMEM;
466 	    krb5_set_error_string(context, "malloc: out of memory");
467 	    goto fail;
468 	}
469 	*a->req_body.from = creds->times.starttime;
470     }
471     if(creds->times.endtime){
472 	ALLOC(a->req_body.till, 1);
473 	*a->req_body.till = creds->times.endtime;
474     }
475     if(creds->times.renew_till){
476 	a->req_body.rtime = malloc(sizeof(*a->req_body.rtime));
477 	if (a->req_body.rtime == NULL) {
478 	    ret = ENOMEM;
479 	    krb5_set_error_string(context, "malloc: out of memory");
480 	    goto fail;
481 	}
482 	*a->req_body.rtime = creds->times.renew_till;
483     }
484     a->req_body.nonce = nonce;
485     ret = krb5_init_etype (context,
486 			   &a->req_body.etype.len,
487 			   &a->req_body.etype.val,
488 			   etypes);
489     if (ret)
490 	goto fail;
491 
492     /*
493      * This means no addresses
494      */
495 
496     if (addrs && addrs->len == 0) {
497 	a->req_body.addresses = NULL;
498     } else {
499 	a->req_body.addresses = malloc(sizeof(*a->req_body.addresses));
500 	if (a->req_body.addresses == NULL) {
501 	    ret = ENOMEM;
502 	    krb5_set_error_string(context, "malloc: out of memory");
503 	    goto fail;
504 	}
505 
506 	if (addrs)
507 	    ret = krb5_copy_addresses(context, addrs, a->req_body.addresses);
508 	else {
509 	    ret = krb5_get_all_client_addrs (context, a->req_body.addresses);
510 	    if(ret == 0 && a->req_body.addresses->len == 0) {
511 		free(a->req_body.addresses);
512 		a->req_body.addresses = NULL;
513 	    }
514 	}
515 	if (ret)
516 	    return ret;
517     }
518 
519     a->req_body.enc_authorization_data = NULL;
520     a->req_body.additional_tickets = NULL;
521 
522     if(preauth != NULL) {
523 	int i;
524 	ALLOC(a->padata, 1);
525 	if(a->padata == NULL) {
526 	    ret = ENOMEM;
527 	    krb5_set_error_string(context, "malloc: out of memory");
528 	    goto fail;
529 	}
530 	for(i = 0; i < preauth->len; i++) {
531 	    if(preauth->val[i].type == KRB5_PADATA_ENC_TIMESTAMP){
532 		int j;
533 		PA_DATA *tmp = realloc(a->padata->val,
534 				       (a->padata->len +
535 					preauth->val[i].info.len) *
536 				       sizeof(*a->padata->val));
537 		if(tmp == NULL) {
538 		    ret = ENOMEM;
539 		    krb5_set_error_string(context, "malloc: out of memory");
540 		    goto fail;
541 		}
542 		a->padata->val = tmp;
543 		for(j = 0; j < preauth->val[i].info.len; j++) {
544 		    krb5_salt *sp = &salt;
545 		    if(preauth->val[i].info.val[j].salttype)
546 			salt.salttype = *preauth->val[i].info.val[j].salttype;
547 		    else
548 			salt.salttype = KRB5_PW_SALT;
549 		    if(preauth->val[i].info.val[j].salt)
550 			salt.saltvalue = *preauth->val[i].info.val[j].salt;
551 		    else
552 			if(salt.salttype == KRB5_PW_SALT)
553 			    sp = NULL;
554 			else
555 			    krb5_data_zero(&salt.saltvalue);
556 		    add_padata(context, a->padata, creds->client,
557 			       key_proc, keyseed,
558 			       &preauth->val[i].info.val[j].etype, 1,
559 			       sp);
560 		}
561 	    }
562 	}
563     } else
564     /* not sure this is the way to use `ptypes' */
565     if (ptypes == NULL || *ptypes == KRB5_PADATA_NONE)
566 	a->padata = NULL;
567     else if (*ptypes ==  KRB5_PADATA_ENC_TIMESTAMP) {
568 	ALLOC(a->padata, 1);
569 	if (a->padata == NULL) {
570 	    ret = ENOMEM;
571 	    krb5_set_error_string(context, "malloc: out of memory");
572 	    goto fail;
573 	}
574 	a->padata->len = 0;
575 	a->padata->val = NULL;
576 
577 	/* make a v5 salted pa-data */
578 	add_padata(context, a->padata, creds->client,
579 		   key_proc, keyseed, a->req_body.etype.val,
580 		   a->req_body.etype.len, NULL);
581 
582 	/* make a v4 salted pa-data */
583 	salt.salttype = KRB5_PW_SALT;
584 	krb5_data_zero(&salt.saltvalue);
585 	add_padata(context, a->padata, creds->client,
586 		   key_proc, keyseed, a->req_body.etype.val,
587 		   a->req_body.etype.len, &salt);
588     } else {
589 	krb5_set_error_string (context, "pre-auth type %d not supported",
590 			       *ptypes);
591 	ret = KRB5_PREAUTH_BAD_TYPE;
592 	goto fail;
593     }
594     return 0;
595 fail:
596     free_AS_REQ(a);
597     return ret;
598 }
599 
600 static int
601 set_ptypes(krb5_context context,
602 	   KRB_ERROR *error,
603 	   krb5_preauthtype **ptypes,
604 	   krb5_preauthdata **preauth)
605 {
606     static krb5_preauthdata preauth2;
607     static krb5_preauthtype ptypes2[] = { KRB5_PADATA_ENC_TIMESTAMP, KRB5_PADATA_NONE };
608 
609     if(error->e_data) {
610 	METHOD_DATA md;
611 	int i;
612 	decode_METHOD_DATA(error->e_data->data,
613 			   error->e_data->length,
614 			   &md,
615 			   NULL);
616 	for(i = 0; i < md.len; i++){
617 	    switch(md.val[i].padata_type){
618 	    case KRB5_PADATA_ENC_TIMESTAMP:
619 		*ptypes = ptypes2;
620 		break;
621 	    case KRB5_PADATA_ETYPE_INFO:
622 		*preauth = &preauth2;
623 		ALLOC_SEQ(*preauth, 1);
624 		(*preauth)->val[0].type = KRB5_PADATA_ENC_TIMESTAMP;
625 		krb5_decode_ETYPE_INFO(context,
626 				       md.val[i].padata_value.data,
627 				       md.val[i].padata_value.length,
628 				       &(*preauth)->val[0].info,
629 				       NULL);
630 		break;
631 	    default:
632 		break;
633 	    }
634 	}
635 	free_METHOD_DATA(&md);
636     } else {
637 	*ptypes = ptypes2;
638     }
639     return(1);
640 }
641 
642 krb5_error_code
643 krb5_get_in_cred(krb5_context context,
644 		 krb5_flags options,
645 		 const krb5_addresses *addrs,
646 		 const krb5_enctype *etypes,
647 		 const krb5_preauthtype *ptypes,
648 		 const krb5_preauthdata *preauth,
649 		 krb5_key_proc key_proc,
650 		 krb5_const_pointer keyseed,
651 		 krb5_decrypt_proc decrypt_proc,
652 		 krb5_const_pointer decryptarg,
653 		 krb5_creds *creds,
654 		 krb5_kdc_rep *ret_as_reply)
655 {
656     krb5_error_code ret;
657     AS_REQ a;
658     krb5_kdc_rep rep;
659     krb5_data req, resp;
660     char buf[BUFSIZ];
661     krb5_salt salt;
662     krb5_keyblock *key;
663     size_t size;
664     krb5_kdc_flags opts;
665     PA_DATA *pa;
666     krb5_enctype etype;
667     krb5_preauthdata *my_preauth = NULL;
668     unsigned nonce;
669     int done;
670 
671     opts.i = options;
672 
673     krb5_generate_random_block (&nonce, sizeof(nonce));
674     nonce &= 0xffffffff;
675 
676     do {
677 	done = 1;
678 	ret = init_as_req (context,
679 			   opts,
680 			   creds,
681 			   addrs,
682 			   etypes,
683 			   ptypes,
684 			   preauth,
685 			   key_proc,
686 			   keyseed,
687 			   nonce,
688 			   &a);
689 	if (my_preauth) {
690 	    free_ETYPE_INFO(&my_preauth->val[0].info);
691 	    free (my_preauth->val);
692 	}
693 	if (ret)
694 	    return ret;
695 
696 	ret = encode_AS_REQ ((unsigned char*)buf + sizeof(buf) - 1,
697 			     sizeof(buf),
698 			     &a,
699 			     &req.length);
700 	free_AS_REQ(&a);
701 	if (ret)
702 	    return ret;
703 
704 	req.data = buf + sizeof(buf) - req.length;
705 
706 	ret = krb5_sendto_kdc (context, &req, &creds->client->realm, &resp);
707 	if (ret)
708 	    return ret;
709 
710 	memset (&rep, 0, sizeof(rep));
711 	ret = decode_AS_REP(resp.data, resp.length, &rep.kdc_rep, &size);
712 	if(ret) {
713 	    /* let's try to parse it as a KRB-ERROR */
714 	    KRB_ERROR error;
715 	    int ret2;
716 
717 	    ret2 = krb5_rd_error(context, &resp, &error);
718 	    if(ret2 && resp.data && ((char*)resp.data)[0] == 4)
719 		ret = KRB5KRB_AP_ERR_V4_REPLY;
720 	    krb5_data_free(&resp);
721 	    if (ret2 == 0) {
722 		ret = krb5_error_from_rd_error(context, &error, creds);
723 		/* if no preauth was set and KDC requires it, give it
724                    one more try */
725 		if (!ptypes && !preauth
726 		    && ret == KRB5KDC_ERR_PREAUTH_REQUIRED
727 #if 0
728 			|| ret == KRB5KDC_ERR_BADOPTION
729 #endif
730 		    && set_ptypes(context, &error, &ptypes, &my_preauth)) {
731 		    done = 0;
732 		    preauth = my_preauth;
733 		    krb5_free_error_contents(context, &error);
734 		    krb5_clear_error_string(context);
735 		    continue;
736 		}
737 		if(ret_as_reply)
738 		    ret_as_reply->error = error;
739 		else
740 		    free_KRB_ERROR (&error);
741 		return ret;
742 	    }
743 	    return ret;
744 	}
745 	krb5_data_free(&resp);
746     } while(!done);
747 
748     pa = NULL;
749     etype = rep.kdc_rep.enc_part.etype;
750     if(rep.kdc_rep.padata){
751 	int index = 0;
752 	pa = krb5_find_padata(rep.kdc_rep.padata->val, rep.kdc_rep.padata->len,
753 			      KRB5_PADATA_PW_SALT, &index);
754 	if(pa == NULL) {
755 	    index = 0;
756 	    pa = krb5_find_padata(rep.kdc_rep.padata->val,
757 				  rep.kdc_rep.padata->len,
758 				  KRB5_PADATA_AFS3_SALT, &index);
759 	}
760     }
761     if(pa) {
762 	salt.salttype = pa->padata_type;
763 	salt.saltvalue = pa->padata_value;
764 
765 	ret = (*key_proc)(context, etype, salt, keyseed, &key);
766     } else {
767 	/* make a v5 salted pa-data */
768 	ret = krb5_get_pw_salt (context, creds->client, &salt);
769 
770 	if (ret)
771 	    goto out;
772 	ret = (*key_proc)(context, etype, salt, keyseed, &key);
773 	krb5_free_salt(context, salt);
774     }
775     if (ret)
776 	goto out;
777 
778     ret = _krb5_extract_ticket(context,
779 			       &rep,
780 			       creds,
781 			       key,
782 			       keyseed,
783 			       KRB5_KU_AS_REP_ENC_PART,
784 			       NULL,
785 			       nonce,
786 			       FALSE,
787 			       opts.b.request_anonymous,
788 			       decrypt_proc,
789 			       decryptarg);
790     memset (key->keyvalue.data, 0, key->keyvalue.length);
791     krb5_free_keyblock_contents (context, key);
792     free (key);
793 
794 out:
795     if (ret == 0 && ret_as_reply)
796 	*ret_as_reply = rep;
797     else
798 	krb5_free_kdc_rep (context, &rep);
799     return ret;
800 }
801 
802 krb5_error_code
803 krb5_get_in_tkt(krb5_context context,
804 		krb5_flags options,
805 		const krb5_addresses *addrs,
806 		const krb5_enctype *etypes,
807 		const krb5_preauthtype *ptypes,
808 		krb5_key_proc key_proc,
809 		krb5_const_pointer keyseed,
810 		krb5_decrypt_proc decrypt_proc,
811 		krb5_const_pointer decryptarg,
812 		krb5_creds *creds,
813 		krb5_ccache ccache,
814 		krb5_kdc_rep *ret_as_reply)
815 {
816     krb5_error_code ret;
817     krb5_kdc_flags opts;
818     opts.i = 0;
819     opts.b = int2KDCOptions(options);
820 
821     ret = krb5_get_in_cred (context,
822 			    opts.i,
823 			    addrs,
824 			    etypes,
825 			    ptypes,
826 			    NULL,
827 			    key_proc,
828 			    keyseed,
829 			    decrypt_proc,
830 			    decryptarg,
831 			    creds,
832 			    ret_as_reply);
833     if(ret)
834 	return ret;
835     ret = krb5_cc_store_cred (context, ccache, creds);
836     krb5_free_creds_contents (context, creds);
837     return ret;
838 }
839