xref: /freebsd/crypto/heimdal/lib/krb5/get_for_creds.c (revision a35d88931c87cfe6bd38f01d7bad22140b3b38f3)
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_for_creds.c,v 1.34.4.1 2004/01/09 00:51:55 lha Exp $");
37 
38 static krb5_error_code
39 add_addrs(krb5_context context,
40 	  krb5_addresses *addr,
41 	  struct addrinfo *ai)
42 {
43     krb5_error_code ret;
44     unsigned n, i;
45     void *tmp;
46     struct addrinfo *a;
47 
48     n = 0;
49     for (a = ai; a != NULL; a = a->ai_next)
50 	++n;
51 
52     tmp = realloc(addr->val, (addr->len + n) * sizeof(*addr->val));
53     if (tmp == NULL) {
54 	krb5_set_error_string(context, "malloc: out of memory");
55 	ret = ENOMEM;
56 	goto fail;
57     }
58     addr->val = tmp;
59     for (i = addr->len; i < (addr->len + n); ++i) {
60 	addr->val[i].addr_type = 0;
61 	krb5_data_zero(&addr->val[i].address);
62     }
63     i = addr->len;
64     for (a = ai; a != NULL; a = a->ai_next) {
65 	krb5_address ad;
66 
67 	ret = krb5_sockaddr2address (context, a->ai_addr, &ad);
68 	if (ret == 0) {
69 	    if (krb5_address_search(context, &ad, addr))
70 		krb5_free_address(context, &ad);
71 	    else
72 		addr->val[i++] = ad;
73 	}
74 	else if (ret == KRB5_PROG_ATYPE_NOSUPP)
75 	    krb5_clear_error_string (context);
76 	else
77 	    goto fail;
78 	addr->len = i;
79     }
80     return 0;
81 fail:
82     krb5_free_addresses (context, addr);
83     return ret;
84 }
85 
86 /*
87  * Forward credentials for `client' to host `hostname`,
88  * making them forwardable if `forwardable', and returning the
89  * blob of data to sent in `out_data'.
90  * If hostname == NULL, pick it from `server'
91  */
92 
93 krb5_error_code
94 krb5_fwd_tgt_creds (krb5_context	context,
95 		    krb5_auth_context	auth_context,
96 		    const char		*hostname,
97 		    krb5_principal	client,
98 		    krb5_principal	server,
99 		    krb5_ccache		ccache,
100 		    int			forwardable,
101 		    krb5_data		*out_data)
102 {
103     krb5_flags flags = 0;
104     krb5_creds creds;
105     krb5_error_code ret;
106     krb5_const_realm client_realm;
107 
108     flags |= KDC_OPT_FORWARDED;
109 
110     if (forwardable)
111 	flags |= KDC_OPT_FORWARDABLE;
112 
113     if (hostname == NULL &&
114 	krb5_principal_get_type(context, server) == KRB5_NT_SRV_HST) {
115 	const char *inst = krb5_principal_get_comp_string(context, server, 0);
116 	const char *host = krb5_principal_get_comp_string(context, server, 1);
117 
118 	if (inst != NULL &&
119 	    strcmp(inst, "host") == 0 &&
120 	    host != NULL &&
121 	    krb5_principal_get_comp_string(context, server, 2) == NULL)
122 	    hostname = host;
123     }
124 
125     client_realm = krb5_principal_get_realm(context, client);
126 
127     memset (&creds, 0, sizeof(creds));
128     creds.client = client;
129 
130     ret = krb5_build_principal(context,
131 			       &creds.server,
132 			       strlen(client_realm),
133 			       client_realm,
134 			       KRB5_TGS_NAME,
135 			       client_realm,
136 			       NULL);
137     if (ret)
138 	return ret;
139 
140     ret = krb5_get_forwarded_creds (context,
141 				    auth_context,
142 				    ccache,
143 				    flags,
144 				    hostname,
145 				    &creds,
146 				    out_data);
147     return ret;
148 }
149 
150 /*
151  *
152  */
153 
154 krb5_error_code
155 krb5_get_forwarded_creds (krb5_context	    context,
156 			  krb5_auth_context auth_context,
157 			  krb5_ccache       ccache,
158 			  krb5_flags        flags,
159 			  const char        *hostname,
160 			  krb5_creds        *in_creds,
161 			  krb5_data         *out_data)
162 {
163     krb5_error_code ret;
164     krb5_creds *out_creds;
165     krb5_addresses addrs, *paddrs;
166     KRB_CRED cred;
167     KrbCredInfo *krb_cred_info;
168     EncKrbCredPart enc_krb_cred_part;
169     size_t len;
170     unsigned char *buf;
171     size_t buf_size;
172     krb5_kdc_flags kdc_flags;
173     krb5_crypto crypto;
174     struct addrinfo *ai;
175     int save_errno;
176     krb5_keyblock *key;
177     krb5_creds *ticket;
178     char *realm;
179 
180     if (in_creds->client && in_creds->client->realm)
181 	realm = in_creds->client->realm;
182     else
183 	realm = in_creds->server->realm;
184 
185     addrs.len = 0;
186     addrs.val = NULL;
187     paddrs = &addrs;
188 
189     /*
190      * If tickets are address-less, forward address-less tickets.
191      */
192 
193     ret = _krb5_get_krbtgt (context,
194 			    ccache,
195 			    realm,
196 			    &ticket);
197     if(ret == 0) {
198 	if (ticket->addresses.len == 0)
199 	    paddrs = NULL;
200 	krb5_free_creds (context, ticket);
201     }
202 
203     if (paddrs != NULL) {
204 
205 	ret = getaddrinfo (hostname, NULL, NULL, &ai);
206 	if (ret) {
207 	    save_errno = errno;
208 	    krb5_set_error_string(context, "resolving %s: %s",
209 				  hostname, gai_strerror(ret));
210 	    return krb5_eai_to_heim_errno(ret, save_errno);
211 	}
212 
213 	ret = add_addrs (context, &addrs, ai);
214 	freeaddrinfo (ai);
215 	if (ret)
216 	    return ret;
217     }
218 
219     kdc_flags.i = flags;
220 
221     ret = krb5_get_kdc_cred (context,
222 			     ccache,
223 			     kdc_flags,
224 			     paddrs,
225 			     NULL,
226 			     in_creds,
227 			     &out_creds);
228     krb5_free_addresses (context, &addrs);
229     if (ret) {
230 	return ret;
231     }
232 
233     memset (&cred, 0, sizeof(cred));
234     cred.pvno = 5;
235     cred.msg_type = krb_cred;
236     ALLOC_SEQ(&cred.tickets, 1);
237     if (cred.tickets.val == NULL) {
238 	ret = ENOMEM;
239 	krb5_set_error_string(context, "malloc: out of memory");
240 	goto out2;
241     }
242     ret = decode_Ticket(out_creds->ticket.data,
243 			out_creds->ticket.length,
244 			cred.tickets.val, &len);
245     if (ret)
246 	goto out3;
247 
248     memset (&enc_krb_cred_part, 0, sizeof(enc_krb_cred_part));
249     ALLOC_SEQ(&enc_krb_cred_part.ticket_info, 1);
250     if (enc_krb_cred_part.ticket_info.val == NULL) {
251 	ret = ENOMEM;
252 	krb5_set_error_string(context, "malloc: out of memory");
253 	goto out4;
254     }
255 
256     if (auth_context->flags & KRB5_AUTH_CONTEXT_DO_TIME) {
257 	int32_t sec, usec;
258 
259 	krb5_us_timeofday (context, &sec, &usec);
260 
261 	ALLOC(enc_krb_cred_part.timestamp, 1);
262 	if (enc_krb_cred_part.timestamp == NULL) {
263 	    ret = ENOMEM;
264 	    krb5_set_error_string(context, "malloc: out of memory");
265 	    goto out4;
266 	}
267 	*enc_krb_cred_part.timestamp = sec;
268 	ALLOC(enc_krb_cred_part.usec, 1);
269 	if (enc_krb_cred_part.usec == NULL) {
270 	    ret = ENOMEM;
271 	    krb5_set_error_string(context, "malloc: out of memory");
272 	    goto out4;
273 	}
274 	*enc_krb_cred_part.usec      = usec;
275     } else {
276 	enc_krb_cred_part.timestamp = NULL;
277 	enc_krb_cred_part.usec = NULL;
278     }
279 
280     if (auth_context->local_address && auth_context->local_port) {
281 	krb5_boolean noaddr;
282 	krb5_const_realm realm;
283 
284 	realm = krb5_principal_get_realm(context, out_creds->server);
285 	krb5_appdefault_boolean(context, NULL, realm, "no-addresses", FALSE,
286 				&noaddr);
287 	if (!noaddr) {
288 	    ret = krb5_make_addrport (context,
289 				      &enc_krb_cred_part.s_address,
290 				      auth_context->local_address,
291 				      auth_context->local_port);
292 	    if (ret)
293 		goto out4;
294 	}
295     }
296 
297     if (auth_context->remote_address) {
298 	if (auth_context->remote_port) {
299 	    krb5_boolean noaddr;
300 	    krb5_const_realm realm;
301 
302 	    realm = krb5_principal_get_realm(context, out_creds->server);
303 	    krb5_appdefault_boolean(context, NULL, realm, "no-addresses",
304 				    FALSE, &noaddr);
305 	    if (!noaddr) {
306 		ret = krb5_make_addrport (context,
307 					  &enc_krb_cred_part.r_address,
308 					  auth_context->remote_address,
309 					  auth_context->remote_port);
310 		if (ret)
311 		    goto out4;
312 	    }
313 	} else {
314 	    ALLOC(enc_krb_cred_part.r_address, 1);
315 	    if (enc_krb_cred_part.r_address == NULL) {
316 		ret = ENOMEM;
317 		krb5_set_error_string(context, "malloc: out of memory");
318 		goto out4;
319 	    }
320 
321 	    ret = krb5_copy_address (context, auth_context->remote_address,
322 				     enc_krb_cred_part.r_address);
323 	    if (ret)
324 		goto out4;
325 	}
326     }
327 
328     /* fill ticket_info.val[0] */
329 
330     enc_krb_cred_part.ticket_info.len = 1;
331 
332     krb_cred_info = enc_krb_cred_part.ticket_info.val;
333 
334     copy_EncryptionKey (&out_creds->session, &krb_cred_info->key);
335     ALLOC(krb_cred_info->prealm, 1);
336     copy_Realm (&out_creds->client->realm, krb_cred_info->prealm);
337     ALLOC(krb_cred_info->pname, 1);
338     copy_PrincipalName(&out_creds->client->name, krb_cred_info->pname);
339     ALLOC(krb_cred_info->flags, 1);
340     *krb_cred_info->flags          = out_creds->flags.b;
341     ALLOC(krb_cred_info->authtime, 1);
342     *krb_cred_info->authtime       = out_creds->times.authtime;
343     ALLOC(krb_cred_info->starttime, 1);
344     *krb_cred_info->starttime      = out_creds->times.starttime;
345     ALLOC(krb_cred_info->endtime, 1);
346     *krb_cred_info->endtime        = out_creds->times.endtime;
347     ALLOC(krb_cred_info->renew_till, 1);
348     *krb_cred_info->renew_till = out_creds->times.renew_till;
349     ALLOC(krb_cred_info->srealm, 1);
350     copy_Realm (&out_creds->server->realm, krb_cred_info->srealm);
351     ALLOC(krb_cred_info->sname, 1);
352     copy_PrincipalName (&out_creds->server->name, krb_cred_info->sname);
353     ALLOC(krb_cred_info->caddr, 1);
354     copy_HostAddresses (&out_creds->addresses, krb_cred_info->caddr);
355 
356     krb5_free_creds (context, out_creds);
357 
358     /* encode EncKrbCredPart */
359 
360     ASN1_MALLOC_ENCODE(EncKrbCredPart, buf, buf_size,
361 		       &enc_krb_cred_part, &len, ret);
362     free_EncKrbCredPart (&enc_krb_cred_part);
363     if (ret) {
364 	free_KRB_CRED(&cred);
365 	return ret;
366     }
367     if(buf_size != len)
368 	krb5_abortx(context, "internal error in ASN.1 encoder");
369 
370     if (auth_context->local_subkey)
371 	key = auth_context->local_subkey;
372     else if (auth_context->remote_subkey)
373 	key = auth_context->remote_subkey;
374     else
375 	key = auth_context->keyblock;
376 
377     ret = krb5_crypto_init(context, key, 0, &crypto);
378     if (ret) {
379 	free(buf);
380 	free_KRB_CRED(&cred);
381 	return ret;
382     }
383     ret = krb5_encrypt_EncryptedData (context,
384 				      crypto,
385 				      KRB5_KU_KRB_CRED,
386 				      buf,
387 				      len,
388 				      0,
389 				      &cred.enc_part);
390     free(buf);
391     krb5_crypto_destroy(context, crypto);
392     if (ret) {
393 	free_KRB_CRED(&cred);
394 	return ret;
395     }
396 
397     ASN1_MALLOC_ENCODE(KRB_CRED, buf, buf_size, &cred, &len, ret);
398     free_KRB_CRED (&cred);
399     if (ret)
400 	return ret;
401     if(buf_size != len)
402 	krb5_abortx(context, "internal error in ASN.1 encoder");
403     out_data->length = len;
404     out_data->data   = buf;
405     return 0;
406  out4:
407     free_EncKrbCredPart(&enc_krb_cred_part);
408  out3:
409     free_KRB_CRED(&cred);
410  out2:
411     krb5_free_creds (context, out_creds);
412     return ret;
413 }
414