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