No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / lib / krb5 / get_for_creds.c
blobf36541d6bd67c453297929fb677784ce5a55083a
1 /*
2 * Copyright (c) 1997 - 2004 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
34 #include <krb5_locl.h>
36 __RCSID("$Heimdal: get_for_creds.c 22504 2008-01-21 15:49:58Z lha $"
37 "$NetBSD$");
39 static krb5_error_code
40 add_addrs(krb5_context context,
41 krb5_addresses *addr,
42 struct addrinfo *ai)
44 krb5_error_code ret;
45 unsigned n, i;
46 void *tmp;
47 struct addrinfo *a;
49 n = 0;
50 for (a = ai; a != NULL; a = a->ai_next)
51 ++n;
53 tmp = realloc(addr->val, (addr->len + n) * sizeof(*addr->val));
54 if (tmp == NULL && (addr->len + n) != 0) {
55 krb5_set_error_string(context, "malloc: out of memory");
56 ret = ENOMEM;
57 goto fail;
59 addr->val = tmp;
60 for (i = addr->len; i < (addr->len + n); ++i) {
61 addr->val[i].addr_type = 0;
62 krb5_data_zero(&addr->val[i].address);
64 i = addr->len;
65 for (a = ai; a != NULL; a = a->ai_next) {
66 krb5_address ad;
68 ret = krb5_sockaddr2address (context, a->ai_addr, &ad);
69 if (ret == 0) {
70 if (krb5_address_search(context, &ad, addr))
71 krb5_free_address(context, &ad);
72 else
73 addr->val[i++] = ad;
75 else if (ret == KRB5_PROG_ATYPE_NOSUPP)
76 krb5_clear_error_string (context);
77 else
78 goto fail;
79 addr->len = i;
81 return 0;
82 fail:
83 krb5_free_addresses (context, addr);
84 return ret;
87 /**
88 * Forward credentials for client to host hostname , making them
89 * forwardable if forwardable, and returning the blob of data to sent
90 * in out_data. If hostname == NULL, pick it from server.
92 * @param context A kerberos 5 context.
93 * @param auth_context the auth context with the key to encrypt the out_data.
94 * @param hostname the host to forward the tickets too.
95 * @param client the client to delegate from.
96 * @param server the server to delegate the credential too.
97 * @param ccache credential cache to use.
98 * @param forwardable make the forwarded ticket forwabledable.
99 * @param out_data the resulting credential.
101 * @return Return an error code or 0.
103 * @ingroup krb5_credential
106 krb5_error_code KRB5_LIB_FUNCTION
107 krb5_fwd_tgt_creds (krb5_context context,
108 krb5_auth_context auth_context,
109 const char *hostname,
110 krb5_principal client,
111 krb5_principal server,
112 krb5_ccache ccache,
113 int forwardable,
114 krb5_data *out_data)
116 krb5_flags flags = 0;
117 krb5_creds creds;
118 krb5_error_code ret;
119 krb5_const_realm client_realm;
121 flags |= KDC_OPT_FORWARDED;
123 if (forwardable)
124 flags |= KDC_OPT_FORWARDABLE;
126 if (hostname == NULL &&
127 krb5_principal_get_type(context, server) == KRB5_NT_SRV_HST) {
128 const char *inst = krb5_principal_get_comp_string(context, server, 0);
129 const char *host = krb5_principal_get_comp_string(context, server, 1);
131 if (inst != NULL &&
132 strcmp(inst, "host") == 0 &&
133 host != NULL &&
134 krb5_principal_get_comp_string(context, server, 2) == NULL)
135 hostname = host;
138 client_realm = krb5_principal_get_realm(context, client);
140 memset (&creds, 0, sizeof(creds));
141 creds.client = client;
143 ret = krb5_build_principal(context,
144 &creds.server,
145 strlen(client_realm),
146 client_realm,
147 KRB5_TGS_NAME,
148 client_realm,
149 NULL);
150 if (ret)
151 return ret;
153 ret = krb5_get_forwarded_creds (context,
154 auth_context,
155 ccache,
156 flags,
157 hostname,
158 &creds,
159 out_data);
160 return ret;
164 * Gets tickets forwarded to hostname. If the tickets that are
165 * forwarded are address-less, the forwarded tickets will also be
166 * address-less.
168 * If the ticket have any address, hostname will be used for figure
169 * out the address to forward the ticket too. This since this might
170 * use DNS, its insecure and also doesn't represent configured all
171 * addresses of the host. For example, the host might have two
172 * adresses, one IPv4 and one IPv6 address where the later is not
173 * published in DNS. This IPv6 address might be used communications
174 * and thus the resulting ticket useless.
176 * @param context A kerberos 5 context.
177 * @param auth_context the auth context with the key to encrypt the out_data.
178 * @param ccache credential cache to use
179 * @param flags the flags to control the resulting ticket flags
180 * @param hostname the host to forward the tickets too.
181 * @param in_creds the in client and server ticket names. The client
182 * and server components forwarded to the remote host.
183 * @param out_data the resulting credential.
185 * @return Return an error code or 0.
187 * @ingroup krb5_credential
190 krb5_error_code KRB5_LIB_FUNCTION
191 krb5_get_forwarded_creds (krb5_context context,
192 krb5_auth_context auth_context,
193 krb5_ccache ccache,
194 krb5_flags flags,
195 const char *hostname,
196 krb5_creds *in_creds,
197 krb5_data *out_data)
199 krb5_error_code ret;
200 krb5_creds *out_creds;
201 krb5_addresses addrs, *paddrs;
202 KRB_CRED cred;
203 KrbCredInfo *krb_cred_info;
204 EncKrbCredPart enc_krb_cred_part;
205 size_t len;
206 unsigned char *buf;
207 size_t buf_size;
208 krb5_kdc_flags kdc_flags;
209 krb5_crypto crypto;
210 struct addrinfo *ai;
211 int save_errno;
212 krb5_creds *ticket;
214 paddrs = NULL;
215 addrs.len = 0;
216 addrs.val = NULL;
218 ret = krb5_get_credentials(context, 0, ccache, in_creds, &ticket);
219 if(ret == 0) {
220 if (ticket->addresses.len)
221 paddrs = &addrs;
222 krb5_free_creds (context, ticket);
223 } else {
224 krb5_boolean noaddr;
225 krb5_appdefault_boolean(context, NULL,
226 krb5_principal_get_realm(context,
227 in_creds->client),
228 "no-addresses", KRB5_ADDRESSLESS_DEFAULT,
229 &noaddr);
230 if (!noaddr)
231 paddrs = &addrs;
235 * If tickets have addresses, get the address of the remote host.
238 if (paddrs != NULL) {
240 ret = getaddrinfo (hostname, NULL, NULL, &ai);
241 if (ret) {
242 save_errno = errno;
243 krb5_set_error_string(context, "resolving %s: %s",
244 hostname, gai_strerror(ret));
245 return krb5_eai_to_heim_errno(ret, save_errno);
248 ret = add_addrs (context, &addrs, ai);
249 freeaddrinfo (ai);
250 if (ret)
251 return ret;
254 kdc_flags.b = int2KDCOptions(flags);
256 ret = krb5_get_kdc_cred (context,
257 ccache,
258 kdc_flags,
259 paddrs,
260 NULL,
261 in_creds,
262 &out_creds);
263 krb5_free_addresses (context, &addrs);
264 if (ret)
265 return ret;
267 memset (&cred, 0, sizeof(cred));
268 cred.pvno = 5;
269 cred.msg_type = krb_cred;
270 ALLOC_SEQ(&cred.tickets, 1);
271 if (cred.tickets.val == NULL) {
272 ret = ENOMEM;
273 krb5_set_error_string(context, "malloc: out of memory");
274 goto out2;
276 ret = decode_Ticket(out_creds->ticket.data,
277 out_creds->ticket.length,
278 cred.tickets.val, &len);
279 if (ret)
280 goto out3;
282 memset (&enc_krb_cred_part, 0, sizeof(enc_krb_cred_part));
283 ALLOC_SEQ(&enc_krb_cred_part.ticket_info, 1);
284 if (enc_krb_cred_part.ticket_info.val == NULL) {
285 ret = ENOMEM;
286 krb5_set_error_string(context, "malloc: out of memory");
287 goto out4;
290 if (auth_context->flags & KRB5_AUTH_CONTEXT_DO_TIME) {
291 krb5_timestamp sec;
292 int32_t usec;
294 krb5_us_timeofday (context, &sec, &usec);
296 ALLOC(enc_krb_cred_part.timestamp, 1);
297 if (enc_krb_cred_part.timestamp == NULL) {
298 ret = ENOMEM;
299 krb5_set_error_string(context, "malloc: out of memory");
300 goto out4;
302 *enc_krb_cred_part.timestamp = sec;
303 ALLOC(enc_krb_cred_part.usec, 1);
304 if (enc_krb_cred_part.usec == NULL) {
305 ret = ENOMEM;
306 krb5_set_error_string(context, "malloc: out of memory");
307 goto out4;
309 *enc_krb_cred_part.usec = usec;
310 } else {
311 enc_krb_cred_part.timestamp = NULL;
312 enc_krb_cred_part.usec = NULL;
315 if (auth_context->local_address && auth_context->local_port && paddrs) {
317 ret = krb5_make_addrport (context,
318 &enc_krb_cred_part.s_address,
319 auth_context->local_address,
320 auth_context->local_port);
321 if (ret)
322 goto out4;
325 if (auth_context->remote_address) {
326 if (auth_context->remote_port) {
327 krb5_boolean noaddr;
328 krb5_const_realm srealm;
330 srealm = krb5_principal_get_realm(context, out_creds->server);
331 /* Is this correct, and should we use the paddrs == NULL
332 trick here as well? Having an address-less ticket may
333 indicate that we don't know our own global address, but
334 it does not necessary mean that we don't know the
335 server's. */
336 krb5_appdefault_boolean(context, NULL, srealm, "no-addresses",
337 FALSE, &noaddr);
338 if (!noaddr) {
339 ret = krb5_make_addrport (context,
340 &enc_krb_cred_part.r_address,
341 auth_context->remote_address,
342 auth_context->remote_port);
343 if (ret)
344 goto out4;
346 } else {
347 ALLOC(enc_krb_cred_part.r_address, 1);
348 if (enc_krb_cred_part.r_address == NULL) {
349 ret = ENOMEM;
350 krb5_set_error_string(context, "malloc: out of memory");
351 goto out4;
354 ret = krb5_copy_address (context, auth_context->remote_address,
355 enc_krb_cred_part.r_address);
356 if (ret)
357 goto out4;
361 /* fill ticket_info.val[0] */
363 enc_krb_cred_part.ticket_info.len = 1;
365 krb_cred_info = enc_krb_cred_part.ticket_info.val;
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);
389 krb5_free_creds (context, out_creds);
391 /* encode EncKrbCredPart */
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;
400 if(buf_size != len)
401 krb5_abortx(context, "internal error in ASN.1 encoder");
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.
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 {
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.
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;
430 ret = krb5_encrypt_EncryptedData (context,
431 crypto,
432 KRB5_KU_KRB_CRED,
433 buf,
434 len,
436 &cred.enc_part);
437 free(buf);
438 krb5_crypto_destroy(context, crypto);
439 if (ret) {
440 free_KRB_CRED(&cred);
441 return ret;
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;