1/*
2 * Copyright (c) 1997 - 2004 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
36static krb5_error_code
37add_addrs(krb5_context context,
38	  krb5_addresses *addr,
39	  struct addrinfo *ai)
40{
41    krb5_error_code ret;
42    unsigned n, i;
43    void *tmp;
44    struct addrinfo *a;
45
46    n = 0;
47    for (a = ai; a != NULL; a = a->ai_next)
48	++n;
49
50    tmp = realloc(addr->val, (addr->len + n) * sizeof(*addr->val));
51    if (tmp == NULL && (addr->len + n) != 0) {
52	ret = ENOMEM;
53	krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
54	goto fail;
55    }
56    addr->val = tmp;
57    for (i = addr->len; i < (addr->len + n); ++i) {
58	addr->val[i].addr_type = 0;
59	krb5_data_zero(&addr->val[i].address);
60    }
61    i = addr->len;
62    for (a = ai; a != NULL; a = a->ai_next) {
63	krb5_address ad;
64
65	ret = krb5_sockaddr2address (context, a->ai_addr, &ad);
66	if (ret == 0) {
67	    if (krb5_address_search(context, &ad, addr))
68		krb5_free_address(context, &ad);
69	    else
70		addr->val[i++] = ad;
71	}
72	else if (ret == KRB5_PROG_ATYPE_NOSUPP)
73	    krb5_clear_error_message (context);
74	else
75	    goto fail;
76	addr->len = i;
77    }
78    return 0;
79fail:
80    krb5_free_addresses (context, addr);
81    return ret;
82}
83
84/**
85 * Forward credentials for client to host hostname , making them
86 * forwardable if forwardable, and returning the blob of data to sent
87 * in out_data.  If hostname == NULL, pick it from server.
88 *
89 * @param context A kerberos 5 context.
90 * @param auth_context the auth context with the key to encrypt the out_data.
91 * @param hostname the host to forward the tickets too.
92 * @param client the client to delegate from.
93 * @param server the server to delegate the credential too.
94 * @param ccache credential cache to use.
95 * @param forwardable make the forwarded ticket forwabledable.
96 * @param out_data the resulting credential.
97 *
98 * @return Return an error code or 0.
99 *
100 * @ingroup krb5_credential
101 */
102
103KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
104krb5_fwd_tgt_creds (krb5_context	context,
105		    krb5_auth_context	auth_context,
106		    const char		*hostname,
107		    krb5_principal	client,
108		    krb5_principal	server,
109		    krb5_ccache		ccache,
110		    int			forwardable,
111		    krb5_data		*out_data)
112{
113    krb5_flags flags = 0;
114    krb5_creds creds;
115    krb5_error_code ret;
116    krb5_const_realm client_realm;
117
118    flags |= KDC_OPT_FORWARDED;
119
120    if (forwardable)
121	flags |= KDC_OPT_FORWARDABLE;
122
123    if (hostname == NULL &&
124	krb5_principal_get_type(context, server) == KRB5_NT_SRV_HST) {
125	const char *inst = krb5_principal_get_comp_string(context, server, 0);
126	const char *host = krb5_principal_get_comp_string(context, server, 1);
127
128	if (inst != NULL &&
129	    strcmp(inst, "host") == 0 &&
130	    host != NULL &&
131	    krb5_principal_get_comp_string(context, server, 2) == NULL)
132	    hostname = host;
133    }
134
135    client_realm = krb5_principal_get_realm(context, client);
136
137    memset (&creds, 0, sizeof(creds));
138    creds.client = client;
139
140    ret = krb5_make_principal(context,
141			      &creds.server,
142			      client_realm,
143			      KRB5_TGS_NAME,
144			      client_realm,
145			      NULL);
146    if (ret)
147	return ret;
148
149    ret = krb5_get_forwarded_creds (context,
150				    auth_context,
151				    ccache,
152				    flags,
153				    hostname,
154				    &creds,
155				    out_data);
156    return ret;
157}
158
159/**
160 * Gets tickets forwarded to hostname. If the tickets that are
161 * forwarded are address-less, the forwarded tickets will also be
162 * address-less.
163 *
164 * If the ticket have any address, hostname will be used for figure
165 * out the address to forward the ticket too. This since this might
166 * use DNS, its insecure and also doesn't represent configured all
167 * addresses of the host. For example, the host might have two
168 * adresses, one IPv4 and one IPv6 address where the later is not
169 * published in DNS. This IPv6 address might be used communications
170 * and thus the resulting ticket useless.
171 *
172 * @param context A kerberos 5 context.
173 * @param auth_context the auth context with the key to encrypt the out_data.
174 * @param ccache credential cache to use
175 * @param flags the flags to control the resulting ticket flags
176 * @param hostname the host to forward the tickets too.
177 * @param in_creds the in client and server ticket names.  The client
178 * and server components forwarded to the remote host.
179 * @param out_data the resulting credential.
180 *
181 * @return Return an error code or 0.
182 *
183 * @ingroup krb5_credential
184 */
185
186KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
187krb5_get_forwarded_creds (krb5_context	    context,
188			  krb5_auth_context auth_context,
189			  krb5_ccache       ccache,
190			  krb5_flags        flags,
191			  const char        *hostname,
192			  krb5_creds        *in_creds,
193			  krb5_data         *out_data)
194{
195    krb5_error_code ret;
196    krb5_creds *out_creds;
197    krb5_addresses addrs, *paddrs;
198    KRB_CRED cred;
199    KrbCredInfo *krb_cred_info;
200    EncKrbCredPart enc_krb_cred_part;
201    size_t len;
202    unsigned char *buf;
203    size_t buf_size;
204    krb5_kdc_flags kdc_flags;
205    krb5_crypto crypto;
206    struct addrinfo *ai;
207    krb5_creds *ticket;
208
209    paddrs = NULL;
210    addrs.len = 0;
211    addrs.val = NULL;
212
213    if (auth_context->keyblock == NULL) {
214	krb5_set_error_message(context, KRB5KDC_ERR_NULL_KEY, N_("auth context is missing session key", ""));
215	return KRB5KDC_ERR_NULL_KEY;
216    }
217
218
219    ret = krb5_get_credentials(context, 0, ccache, in_creds, &ticket);
220    if(ret == 0) {
221	if (ticket->addresses.len)
222	    paddrs = &addrs;
223	krb5_free_creds (context, ticket);
224    } else {
225	krb5_boolean noaddr;
226	krb5_appdefault_boolean(context, NULL,
227				krb5_principal_get_realm(context,
228							 in_creds->client),
229				"no-addresses", KRB5_ADDRESSLESS_DEFAULT,
230				&noaddr);
231	if (!noaddr)
232	    paddrs = &addrs;
233    }
234
235    /*
236     * If tickets have addresses, get the address of the remote host.
237     */
238
239    if (paddrs != NULL) {
240
241	ret = getaddrinfo (hostname, NULL, NULL, &ai);
242	if (ret) {
243	    krb5_error_code ret2 = krb5_eai_to_heim_errno(ret, errno);
244	    krb5_set_error_message(context, ret2,
245				   N_("resolving host %s failed: %s",
246				      "hostname, error"),
247				  hostname, gai_strerror(ret));
248	    return ret2;
249	}
250
251	ret = add_addrs (context, &addrs, ai);
252	freeaddrinfo (ai);
253	if (ret)
254	    return ret;
255    }
256
257    kdc_flags.b = int2KDCOptions(flags);
258
259    ret = krb5_get_kdc_cred (context,
260			     ccache,
261			     kdc_flags,
262			     paddrs,
263			     NULL,
264			     in_creds,
265			     &out_creds);
266    krb5_free_addresses (context, &addrs);
267    if (ret)
268	return ret;
269
270    memset (&cred, 0, sizeof(cred));
271    cred.pvno = 5;
272    cred.msg_type = krb_cred;
273    ALLOC_SEQ(&cred.tickets, 1);
274    if (cred.tickets.val == NULL) {
275	ret = ENOMEM;
276	krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
277	goto out2;
278    }
279    ret = decode_Ticket(out_creds->ticket.data,
280			out_creds->ticket.length,
281			cred.tickets.val, &len);
282    if (ret)
283	goto out3;
284
285    memset (&enc_krb_cred_part, 0, sizeof(enc_krb_cred_part));
286    ALLOC_SEQ(&enc_krb_cred_part.ticket_info, 1);
287    if (enc_krb_cred_part.ticket_info.val == NULL) {
288	ret = ENOMEM;
289	krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
290	goto out4;
291    }
292
293    if (auth_context->flags & KRB5_AUTH_CONTEXT_DO_TIME) {
294	krb5_timestamp sec;
295	int32_t usec;
296
297	krb5_us_timeofday (context, &sec, &usec);
298
299	ALLOC(enc_krb_cred_part.timestamp, 1);
300	if (enc_krb_cred_part.timestamp == NULL) {
301	    ret = ENOMEM;
302	    krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
303	    goto out4;
304	}
305	*enc_krb_cred_part.timestamp = sec;
306	ALLOC(enc_krb_cred_part.usec, 1);
307	if (enc_krb_cred_part.usec == NULL) {
308	    ret = ENOMEM;
309	    krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
310	    goto out4;
311	}
312	*enc_krb_cred_part.usec      = usec;
313    } else {
314	enc_krb_cred_part.timestamp = NULL;
315	enc_krb_cred_part.usec = NULL;
316    }
317
318    if (auth_context->local_address && auth_context->local_port && paddrs) {
319
320	ret = krb5_make_addrport (context,
321				  &enc_krb_cred_part.s_address,
322				  auth_context->local_address,
323				  auth_context->local_port);
324	if (ret)
325	    goto out4;
326    }
327
328    if (auth_context->remote_address) {
329	if (auth_context->remote_port) {
330	    krb5_boolean noaddr;
331	    krb5_const_realm srealm;
332
333	    srealm = krb5_principal_get_realm(context, out_creds->server);
334	    /* Is this correct, and should we use the paddrs == NULL
335               trick here as well? Having an address-less ticket may
336               indicate that we don't know our own global address, but
337               it does not necessary mean that we don't know the
338               server's. */
339	    krb5_appdefault_boolean(context, NULL, srealm, "no-addresses",
340				    FALSE, &noaddr);
341	    if (!noaddr) {
342		ret = krb5_make_addrport (context,
343					  &enc_krb_cred_part.r_address,
344					  auth_context->remote_address,
345					  auth_context->remote_port);
346		if (ret)
347		    goto out4;
348	    }
349	} else {
350	    ALLOC(enc_krb_cred_part.r_address, 1);
351	    if (enc_krb_cred_part.r_address == NULL) {
352		ret = ENOMEM;
353		krb5_set_error_message(context, ret,
354				       N_("malloc: out of memory", ""));
355		goto out4;
356	    }
357
358	    ret = krb5_copy_address (context, auth_context->remote_address,
359				     enc_krb_cred_part.r_address);
360	    if (ret)
361		goto out4;
362	}
363    }
364
365    /* fill ticket_info.val[0] */
366
367    enc_krb_cred_part.ticket_info.len = 1;
368
369    krb_cred_info = enc_krb_cred_part.ticket_info.val;
370
371    copy_EncryptionKey (&out_creds->session, &krb_cred_info->key);
372    ALLOC(krb_cred_info->prealm, 1);
373    copy_Realm (&out_creds->client->realm, krb_cred_info->prealm);
374    ALLOC(krb_cred_info->pname, 1);
375    copy_PrincipalName(&out_creds->client->name, krb_cred_info->pname);
376    ALLOC(krb_cred_info->flags, 1);
377    *krb_cred_info->flags          = out_creds->flags.b;
378    ALLOC(krb_cred_info->authtime, 1);
379    *krb_cred_info->authtime       = out_creds->times.authtime;
380    ALLOC(krb_cred_info->starttime, 1);
381    *krb_cred_info->starttime      = out_creds->times.starttime;
382    ALLOC(krb_cred_info->endtime, 1);
383    *krb_cred_info->endtime        = out_creds->times.endtime;
384    ALLOC(krb_cred_info->renew_till, 1);
385    *krb_cred_info->renew_till = out_creds->times.renew_till;
386    ALLOC(krb_cred_info->srealm, 1);
387    copy_Realm (&out_creds->server->realm, krb_cred_info->srealm);
388    ALLOC(krb_cred_info->sname, 1);
389    copy_PrincipalName (&out_creds->server->name, krb_cred_info->sname);
390    ALLOC(krb_cred_info->caddr, 1);
391    copy_HostAddresses (&out_creds->addresses, krb_cred_info->caddr);
392
393    krb5_free_creds (context, out_creds);
394
395    /* encode EncKrbCredPart */
396
397    ASN1_MALLOC_ENCODE(EncKrbCredPart, buf, buf_size,
398		       &enc_krb_cred_part, &len, ret);
399    free_EncKrbCredPart (&enc_krb_cred_part);
400    if (ret) {
401	free_KRB_CRED(&cred);
402	return ret;
403    }
404    if(buf_size != len)
405	krb5_abortx(context, "internal error in ASN.1 encoder");
406
407    /*
408     * Here older versions then 0.7.2 of Heimdal used the local or
409     * remote subkey. That is wrong, the session key should be
410     * used. Heimdal 0.7.2 and newer have code to try both in the
411     * receiving end.
412     */
413
414    ret = krb5_crypto_init(context, auth_context->keyblock, 0, &crypto);
415    if (ret) {
416	free(buf);
417	free_KRB_CRED(&cred);
418	return ret;
419    }
420    ret = krb5_encrypt_EncryptedData (context,
421				      crypto,
422				      KRB5_KU_KRB_CRED,
423				      buf,
424				      len,
425				      0,
426				      &cred.enc_part);
427    free(buf);
428    krb5_crypto_destroy(context, crypto);
429    if (ret) {
430	free_KRB_CRED(&cred);
431	return ret;
432    }
433
434    ASN1_MALLOC_ENCODE(KRB_CRED, buf, buf_size, &cred, &len, ret);
435    free_KRB_CRED (&cred);
436    if (ret)
437	return ret;
438    if(buf_size != len)
439	krb5_abortx(context, "internal error in ASN.1 encoder");
440    out_data->length = len;
441    out_data->data   = buf;
442    return 0;
443 out4:
444    free_EncKrbCredPart(&enc_krb_cred_part);
445 out3:
446    free_KRB_CRED(&cred);
447 out2:
448    krb5_free_creds (context, out_creds);
449    return ret;
450}
451