xref: /netbsd-src/crypto/external/bsd/heimdal/dist/lib/krb5/get_in_tkt.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: get_in_tkt.c,v 1.1.1.1 2011/04/13 18:15:34 elric Exp $	*/
2 
3 /*
4  * Copyright (c) 1997 - 2008 Kungliga Tekniska Högskolan
5  * (Royal Institute of Technology, Stockholm, Sweden).
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * 3. Neither the name of the Institute nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #define KRB5_DEPRECATED
37 
38 #include "krb5_locl.h"
39 
40 #ifndef HEIMDAL_SMALLER
41 
42 static krb5_error_code
43 make_pa_enc_timestamp(krb5_context context, PA_DATA *pa,
44 		      krb5_enctype etype, krb5_keyblock *key)
45 {
46     PA_ENC_TS_ENC p;
47     unsigned char *buf;
48     size_t buf_size;
49     size_t len;
50     EncryptedData encdata;
51     krb5_error_code ret;
52     int32_t usec;
53     int usec2;
54     krb5_crypto crypto;
55 
56     krb5_us_timeofday (context, &p.patimestamp, &usec);
57     usec2         = usec;
58     p.pausec      = &usec2;
59 
60     ASN1_MALLOC_ENCODE(PA_ENC_TS_ENC, buf, buf_size, &p, &len, ret);
61     if (ret)
62 	return ret;
63     if(buf_size != len)
64 	krb5_abortx(context, "internal error in ASN.1 encoder");
65     ret = krb5_crypto_init(context, key, 0, &crypto);
66     if (ret) {
67 	free(buf);
68 	return ret;
69     }
70     ret = krb5_encrypt_EncryptedData(context,
71 				     crypto,
72 				     KRB5_KU_PA_ENC_TIMESTAMP,
73 				     buf,
74 				     len,
75 				     0,
76 				     &encdata);
77     free(buf);
78     krb5_crypto_destroy(context, crypto);
79     if (ret)
80 	return ret;
81 
82     ASN1_MALLOC_ENCODE(EncryptedData, buf, buf_size, &encdata, &len, ret);
83     free_EncryptedData(&encdata);
84     if (ret)
85 	return ret;
86     if(buf_size != len)
87 	krb5_abortx(context, "internal error in ASN.1 encoder");
88     pa->padata_type = KRB5_PADATA_ENC_TIMESTAMP;
89     pa->padata_value.length = len;
90     pa->padata_value.data = buf;
91     return 0;
92 }
93 
94 static krb5_error_code
95 add_padata(krb5_context context,
96 	   METHOD_DATA *md,
97 	   krb5_principal client,
98 	   krb5_key_proc key_proc,
99 	   krb5_const_pointer keyseed,
100 	   krb5_enctype *enctypes,
101 	   unsigned netypes,
102 	   krb5_salt *salt)
103 {
104     krb5_error_code ret;
105     PA_DATA *pa2;
106     krb5_salt salt2;
107     krb5_enctype *ep;
108     int i;
109 
110     if(salt == NULL) {
111 	/* default to standard salt */
112 	ret = krb5_get_pw_salt (context, client, &salt2);
113 	if (ret)
114 	    return ret;
115 	salt = &salt2;
116     }
117     if (!enctypes) {
118 	enctypes = context->etypes;
119 	netypes = 0;
120 	for (ep = enctypes; *ep != ETYPE_NULL; ep++)
121 	    netypes++;
122     }
123     pa2 = realloc (md->val, (md->len + netypes) * sizeof(*md->val));
124     if (pa2 == NULL) {
125 	krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
126 	return ENOMEM;
127     }
128     md->val = pa2;
129 
130     for (i = 0; i < netypes; ++i) {
131 	krb5_keyblock *key;
132 
133 	ret = (*key_proc)(context, enctypes[i], *salt, keyseed, &key);
134 	if (ret)
135 	    continue;
136 	ret = make_pa_enc_timestamp (context, &md->val[md->len],
137 				     enctypes[i], key);
138 	krb5_free_keyblock (context, key);
139 	if (ret)
140 	    return ret;
141 	++md->len;
142     }
143     if(salt == &salt2)
144 	krb5_free_salt(context, salt2);
145     return 0;
146 }
147 
148 static krb5_error_code
149 init_as_req (krb5_context context,
150 	     KDCOptions opts,
151 	     krb5_creds *creds,
152 	     const krb5_addresses *addrs,
153 	     const krb5_enctype *etypes,
154 	     const krb5_preauthtype *ptypes,
155 	     const krb5_preauthdata *preauth,
156 	     krb5_key_proc key_proc,
157 	     krb5_const_pointer keyseed,
158 	     unsigned nonce,
159 	     AS_REQ *a)
160 {
161     krb5_error_code ret;
162     krb5_salt salt;
163 
164     memset(a, 0, sizeof(*a));
165 
166     a->pvno = 5;
167     a->msg_type = krb_as_req;
168     a->req_body.kdc_options = opts;
169     a->req_body.cname = malloc(sizeof(*a->req_body.cname));
170     if (a->req_body.cname == NULL) {
171 	ret = ENOMEM;
172 	krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
173 	goto fail;
174     }
175     a->req_body.sname = malloc(sizeof(*a->req_body.sname));
176     if (a->req_body.sname == NULL) {
177 	ret = ENOMEM;
178 	krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
179 	goto fail;
180     }
181     ret = _krb5_principal2principalname (a->req_body.cname, creds->client);
182     if (ret)
183 	goto fail;
184     ret = _krb5_principal2principalname (a->req_body.sname, creds->server);
185     if (ret)
186 	goto fail;
187     ret = copy_Realm(&creds->client->realm, &a->req_body.realm);
188     if (ret)
189 	goto fail;
190 
191     if(creds->times.starttime) {
192 	a->req_body.from = malloc(sizeof(*a->req_body.from));
193 	if (a->req_body.from == NULL) {
194 	    ret = ENOMEM;
195 	    krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
196 	    goto fail;
197 	}
198 	*a->req_body.from = creds->times.starttime;
199     }
200     if(creds->times.endtime){
201 	ALLOC(a->req_body.till, 1);
202 	*a->req_body.till = creds->times.endtime;
203     }
204     if(creds->times.renew_till){
205 	a->req_body.rtime = malloc(sizeof(*a->req_body.rtime));
206 	if (a->req_body.rtime == NULL) {
207 	    ret = ENOMEM;
208 	    krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
209 	    goto fail;
210 	}
211 	*a->req_body.rtime = creds->times.renew_till;
212     }
213     a->req_body.nonce = nonce;
214     ret = krb5_init_etype (context,
215 			   &a->req_body.etype.len,
216 			   &a->req_body.etype.val,
217 			   etypes);
218     if (ret)
219 	goto fail;
220 
221     /*
222      * This means no addresses
223      */
224 
225     if (addrs && addrs->len == 0) {
226 	a->req_body.addresses = NULL;
227     } else {
228 	a->req_body.addresses = malloc(sizeof(*a->req_body.addresses));
229 	if (a->req_body.addresses == NULL) {
230 	    ret = ENOMEM;
231 	    krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
232 	    goto fail;
233 	}
234 
235 	if (addrs)
236 	    ret = krb5_copy_addresses(context, addrs, a->req_body.addresses);
237 	else {
238 	    ret = krb5_get_all_client_addrs (context, a->req_body.addresses);
239 	    if(ret == 0 && a->req_body.addresses->len == 0) {
240 		free(a->req_body.addresses);
241 		a->req_body.addresses = NULL;
242 	    }
243 	}
244 	if (ret)
245 	    return ret;
246     }
247 
248     a->req_body.enc_authorization_data = NULL;
249     a->req_body.additional_tickets = NULL;
250 
251     if(preauth != NULL) {
252 	int i;
253 	ALLOC(a->padata, 1);
254 	if(a->padata == NULL) {
255 	    ret = ENOMEM;
256 	    krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
257 	    goto fail;
258 	}
259 	a->padata->val = NULL;
260 	a->padata->len = 0;
261 	for(i = 0; i < preauth->len; i++) {
262 	    if(preauth->val[i].type == KRB5_PADATA_ENC_TIMESTAMP){
263 		int j;
264 
265 		for(j = 0; j < preauth->val[i].info.len; j++) {
266 		    krb5_salt *sp = &salt;
267 		    if(preauth->val[i].info.val[j].salttype)
268 			salt.salttype = *preauth->val[i].info.val[j].salttype;
269 		    else
270 			salt.salttype = KRB5_PW_SALT;
271 		    if(preauth->val[i].info.val[j].salt)
272 			salt.saltvalue = *preauth->val[i].info.val[j].salt;
273 		    else
274 			if(salt.salttype == KRB5_PW_SALT)
275 			    sp = NULL;
276 			else
277 			    krb5_data_zero(&salt.saltvalue);
278 		    ret = add_padata(context, a->padata, creds->client,
279 				     key_proc, keyseed,
280 				     &preauth->val[i].info.val[j].etype, 1,
281 				     sp);
282 		    if (ret == 0)
283 			break;
284 		}
285 	    }
286 	}
287     } else
288     /* not sure this is the way to use `ptypes' */
289     if (ptypes == NULL || *ptypes == KRB5_PADATA_NONE)
290 	a->padata = NULL;
291     else if (*ptypes ==  KRB5_PADATA_ENC_TIMESTAMP) {
292 	ALLOC(a->padata, 1);
293 	if (a->padata == NULL) {
294 	    ret = ENOMEM;
295 	    krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
296 	    goto fail;
297 	}
298 	a->padata->len = 0;
299 	a->padata->val = NULL;
300 
301 	/* make a v5 salted pa-data */
302 	add_padata(context, a->padata, creds->client,
303 		   key_proc, keyseed, a->req_body.etype.val,
304 		   a->req_body.etype.len, NULL);
305 
306 	/* make a v4 salted pa-data */
307 	salt.salttype = KRB5_PW_SALT;
308 	krb5_data_zero(&salt.saltvalue);
309 	add_padata(context, a->padata, creds->client,
310 		   key_proc, keyseed, a->req_body.etype.val,
311 		   a->req_body.etype.len, &salt);
312     } else {
313 	ret = KRB5_PREAUTH_BAD_TYPE;
314 	krb5_set_error_message (context, ret,
315 				N_("pre-auth type %d not supported", ""),
316 			       *ptypes);
317 	goto fail;
318     }
319     return 0;
320 fail:
321     free_AS_REQ(a);
322     return ret;
323 }
324 
325 static int
326 set_ptypes(krb5_context context,
327 	   KRB_ERROR *error,
328 	   const krb5_preauthtype **ptypes,
329 	   krb5_preauthdata **preauth)
330 {
331     static krb5_preauthdata preauth2;
332     static krb5_preauthtype ptypes2[] = { KRB5_PADATA_ENC_TIMESTAMP, KRB5_PADATA_NONE };
333 
334     if(error->e_data) {
335 	METHOD_DATA md;
336 	int i;
337 	decode_METHOD_DATA(error->e_data->data,
338 			   error->e_data->length,
339 			   &md,
340 			   NULL);
341 	for(i = 0; i < md.len; i++){
342 	    switch(md.val[i].padata_type){
343 	    case KRB5_PADATA_ENC_TIMESTAMP:
344 		*ptypes = ptypes2;
345 		break;
346 	    case KRB5_PADATA_ETYPE_INFO:
347 		*preauth = &preauth2;
348 		ALLOC_SEQ(*preauth, 1);
349 		(*preauth)->val[0].type = KRB5_PADATA_ENC_TIMESTAMP;
350 		decode_ETYPE_INFO(md.val[i].padata_value.data,
351 				  md.val[i].padata_value.length,
352 				  &(*preauth)->val[0].info,
353 				  NULL);
354 		break;
355 	    default:
356 		break;
357 	    }
358 	}
359 	free_METHOD_DATA(&md);
360     } else {
361 	*ptypes = ptypes2;
362     }
363     return(1);
364 }
365 
366 KRB5_DEPRECATED
367 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
368 krb5_get_in_cred(krb5_context context,
369 		 krb5_flags options,
370 		 const krb5_addresses *addrs,
371 		 const krb5_enctype *etypes,
372 		 const krb5_preauthtype *ptypes,
373 		 const krb5_preauthdata *preauth,
374 		 krb5_key_proc key_proc,
375 		 krb5_const_pointer keyseed,
376 		 krb5_decrypt_proc decrypt_proc,
377 		 krb5_const_pointer decryptarg,
378 		 krb5_creds *creds,
379 		 krb5_kdc_rep *ret_as_reply)
380 {
381     krb5_error_code ret;
382     AS_REQ a;
383     krb5_kdc_rep rep;
384     krb5_data req, resp;
385     size_t len;
386     krb5_salt salt;
387     krb5_keyblock *key;
388     size_t size;
389     KDCOptions opts;
390     PA_DATA *pa;
391     krb5_enctype etype;
392     krb5_preauthdata *my_preauth = NULL;
393     unsigned nonce;
394     int done;
395 
396     opts = int2KDCOptions(options);
397 
398     krb5_generate_random_block (&nonce, sizeof(nonce));
399     nonce &= 0xffffffff;
400 
401     do {
402 	done = 1;
403 	ret = init_as_req (context,
404 			   opts,
405 			   creds,
406 			   addrs,
407 			   etypes,
408 			   ptypes,
409 			   preauth,
410 			   key_proc,
411 			   keyseed,
412 			   nonce,
413 			   &a);
414 	if (my_preauth) {
415 	    free_ETYPE_INFO(&my_preauth->val[0].info);
416 	    free (my_preauth->val);
417 	    my_preauth = NULL;
418 	}
419 	if (ret)
420 	    return ret;
421 
422 	ASN1_MALLOC_ENCODE(AS_REQ, req.data, req.length, &a, &len, ret);
423 	free_AS_REQ(&a);
424 	if (ret)
425 	    return ret;
426 	if(len != req.length)
427 	    krb5_abortx(context, "internal error in ASN.1 encoder");
428 
429 	ret = krb5_sendto_kdc (context, &req, &creds->client->realm, &resp);
430 	krb5_data_free(&req);
431 	if (ret)
432 	    return ret;
433 
434 	memset (&rep, 0, sizeof(rep));
435 	ret = decode_AS_REP(resp.data, resp.length, &rep.kdc_rep, &size);
436 	if(ret) {
437 	    /* let's try to parse it as a KRB-ERROR */
438 	    KRB_ERROR error;
439 	    int ret2;
440 
441 	    ret2 = krb5_rd_error(context, &resp, &error);
442 	    if(ret2 && resp.data && ((char*)resp.data)[0] == 4)
443 		ret = KRB5KRB_AP_ERR_V4_REPLY;
444 	    krb5_data_free(&resp);
445 	    if (ret2 == 0) {
446 		ret = krb5_error_from_rd_error(context, &error, creds);
447 		/* if no preauth was set and KDC requires it, give it
448                    one more try */
449 		if (!ptypes && !preauth
450 		    && ret == KRB5KDC_ERR_PREAUTH_REQUIRED
451 #if 0
452 			|| ret == KRB5KDC_ERR_BADOPTION
453 #endif
454 		    && set_ptypes(context, &error, &ptypes, &my_preauth)) {
455 		    done = 0;
456 		    preauth = my_preauth;
457 		    krb5_free_error_contents(context, &error);
458 		    krb5_clear_error_message(context);
459 		    continue;
460 		}
461 		if(ret_as_reply)
462 		    ret_as_reply->error = error;
463 		else
464 		    free_KRB_ERROR (&error);
465 		return ret;
466 	    }
467 	    return ret;
468 	}
469 	krb5_data_free(&resp);
470     } while(!done);
471 
472     pa = NULL;
473     etype = rep.kdc_rep.enc_part.etype;
474     if(rep.kdc_rep.padata){
475 	int i = 0;
476 	pa = krb5_find_padata(rep.kdc_rep.padata->val, rep.kdc_rep.padata->len,
477 			      KRB5_PADATA_PW_SALT, &i);
478 	if(pa == NULL) {
479 	    i = 0;
480 	    pa = krb5_find_padata(rep.kdc_rep.padata->val,
481 				  rep.kdc_rep.padata->len,
482 				  KRB5_PADATA_AFS3_SALT, &i);
483 	}
484     }
485     if(pa) {
486 	salt.salttype = pa->padata_type;
487 	salt.saltvalue = pa->padata_value;
488 
489 	ret = (*key_proc)(context, etype, salt, keyseed, &key);
490     } else {
491 	/* make a v5 salted pa-data */
492 	ret = krb5_get_pw_salt (context, creds->client, &salt);
493 
494 	if (ret)
495 	    goto out;
496 	ret = (*key_proc)(context, etype, salt, keyseed, &key);
497 	krb5_free_salt(context, salt);
498     }
499     if (ret)
500 	goto out;
501 
502     {
503 	unsigned flags = EXTRACT_TICKET_TIMESYNC;
504 	if (opts.request_anonymous)
505 	    flags |= EXTRACT_TICKET_ALLOW_SERVER_MISMATCH;
506 
507 	ret = _krb5_extract_ticket(context,
508 				   &rep,
509 				   creds,
510 				   key,
511 				   keyseed,
512 				   KRB5_KU_AS_REP_ENC_PART,
513 				   NULL,
514 				   nonce,
515 				   flags,
516 				   decrypt_proc,
517 				   decryptarg);
518     }
519     memset (key->keyvalue.data, 0, key->keyvalue.length);
520     krb5_free_keyblock_contents (context, key);
521     free (key);
522 
523 out:
524     if (ret == 0 && ret_as_reply)
525 	*ret_as_reply = rep;
526     else
527 	krb5_free_kdc_rep (context, &rep);
528     return ret;
529 }
530 
531 KRB5_DEPRECATED
532 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
533 krb5_get_in_tkt(krb5_context context,
534 		krb5_flags options,
535 		const krb5_addresses *addrs,
536 		const krb5_enctype *etypes,
537 		const krb5_preauthtype *ptypes,
538 		krb5_key_proc key_proc,
539 		krb5_const_pointer keyseed,
540 		krb5_decrypt_proc decrypt_proc,
541 		krb5_const_pointer decryptarg,
542 		krb5_creds *creds,
543 		krb5_ccache ccache,
544 		krb5_kdc_rep *ret_as_reply)
545 {
546     krb5_error_code ret;
547 
548     ret = krb5_get_in_cred (context,
549 			    options,
550 			    addrs,
551 			    etypes,
552 			    ptypes,
553 			    NULL,
554 			    key_proc,
555 			    keyseed,
556 			    decrypt_proc,
557 			    decryptarg,
558 			    creds,
559 			    ret_as_reply);
560     if(ret)
561 	return ret;
562     if (ccache)
563 	ret = krb5_cc_store_cred (context, ccache, creds);
564     return ret;
565 }
566 
567 #endif /* HEIMDAL_SMALLER */
568