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