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