NULL_RETURNS paranoid check [CID-66]
[heimdal.git] / lib / krb5 / get_for_creds.c
blob19e48173df92b0d17873c406286257033504c76e
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 static krb5_error_code
37 add_addrs(krb5_context context,
38 krb5_addresses *addr,
39 struct addrinfo *ai)
41 krb5_error_code ret;
42 unsigned n, i;
43 void *tmp;
44 struct addrinfo *a;
46 n = 0;
47 for (a = ai; a != NULL; a = a->ai_next)
48 ++n;
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;
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);
61 i = addr->len;
62 for (a = ai; a != NULL; a = a->ai_next) {
63 krb5_address ad;
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;
72 else if (ret == KRB5_PROG_ATYPE_NOSUPP)
73 krb5_clear_error_message (context);
74 else
75 goto fail;
76 addr->len = i;
78 return 0;
79 fail:
80 krb5_free_addresses (context, addr);
81 return ret;
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.
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.
98 * @return Return an error code or 0.
100 * @ingroup krb5_credential
103 krb5_error_code KRB5_LIB_FUNCTION
104 krb5_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)
113 krb5_flags flags = 0;
114 krb5_creds creds;
115 krb5_error_code ret;
116 krb5_const_realm client_realm;
118 flags |= KDC_OPT_FORWARDED;
120 if (forwardable)
121 flags |= KDC_OPT_FORWARDABLE;
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);
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;
135 client_realm = krb5_principal_get_realm(context, client);
137 memset (&creds, 0, sizeof(creds));
138 creds.client = client;
140 ret = krb5_build_principal(context,
141 &creds.server,
142 strlen(client_realm),
143 client_realm,
144 KRB5_TGS_NAME,
145 client_realm,
146 NULL);
147 if (ret)
148 return ret;
150 ret = krb5_get_forwarded_creds (context,
151 auth_context,
152 ccache,
153 flags,
154 hostname,
155 &creds,
156 out_data);
157 return ret;
161 * Gets tickets forwarded to hostname. If the tickets that are
162 * forwarded are address-less, the forwarded tickets will also be
163 * address-less.
165 * If the ticket have any address, hostname will be used for figure
166 * out the address to forward the ticket too. This since this might
167 * use DNS, its insecure and also doesn't represent configured all
168 * addresses of the host. For example, the host might have two
169 * adresses, one IPv4 and one IPv6 address where the later is not
170 * published in DNS. This IPv6 address might be used communications
171 * and thus the resulting ticket useless.
173 * @param context A kerberos 5 context.
174 * @param auth_context the auth context with the key to encrypt the out_data.
175 * @param ccache credential cache to use
176 * @param flags the flags to control the resulting ticket flags
177 * @param hostname the host to forward the tickets too.
178 * @param in_creds the in client and server ticket names. The client
179 * and server components forwarded to the remote host.
180 * @param out_data the resulting credential.
182 * @return Return an error code or 0.
184 * @ingroup krb5_credential
187 krb5_error_code KRB5_LIB_FUNCTION
188 krb5_get_forwarded_creds (krb5_context context,
189 krb5_auth_context auth_context,
190 krb5_ccache ccache,
191 krb5_flags flags,
192 const char *hostname,
193 krb5_creds *in_creds,
194 krb5_data *out_data)
196 krb5_error_code ret;
197 krb5_creds *out_creds;
198 krb5_addresses addrs, *paddrs;
199 KRB_CRED cred;
200 KrbCredInfo *krb_cred_info;
201 EncKrbCredPart enc_krb_cred_part;
202 size_t len;
203 unsigned char *buf;
204 size_t buf_size;
205 krb5_kdc_flags kdc_flags;
206 krb5_crypto crypto;
207 struct addrinfo *ai;
208 krb5_creds *ticket;
210 paddrs = NULL;
211 addrs.len = 0;
212 addrs.val = NULL;
214 ret = krb5_get_credentials(context, 0, ccache, in_creds, &ticket);
215 if(ret == 0) {
216 if (ticket->addresses.len)
217 paddrs = &addrs;
218 krb5_free_creds (context, ticket);
219 } else {
220 krb5_boolean noaddr;
221 krb5_appdefault_boolean(context, NULL,
222 krb5_principal_get_realm(context,
223 in_creds->client),
224 "no-addresses", KRB5_ADDRESSLESS_DEFAULT,
225 &noaddr);
226 if (!noaddr)
227 paddrs = &addrs;
231 * If tickets have addresses, get the address of the remote host.
234 if (paddrs != NULL) {
236 ret = getaddrinfo (hostname, NULL, NULL, &ai);
237 if (ret) {
238 krb5_error_code ret2 = krb5_eai_to_heim_errno(ret, errno);
239 krb5_set_error_message(context, ret2,
240 N_("resolving host %s failed: %s",
241 "hostname, error"),
242 hostname, gai_strerror(ret));
243 return ret2;
246 ret = add_addrs (context, &addrs, ai);
247 freeaddrinfo (ai);
248 if (ret)
249 return ret;
252 kdc_flags.b = int2KDCOptions(flags);
254 ret = krb5_get_kdc_cred (context,
255 ccache,
256 kdc_flags,
257 paddrs,
258 NULL,
259 in_creds,
260 &out_creds);
261 krb5_free_addresses (context, &addrs);
262 if (ret)
263 return ret;
265 memset (&cred, 0, sizeof(cred));
266 cred.pvno = 5;
267 cred.msg_type = krb_cred;
268 ALLOC_SEQ(&cred.tickets, 1);
269 if (cred.tickets.val == NULL) {
270 ret = ENOMEM;
271 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
272 goto out2;
274 ret = decode_Ticket(out_creds->ticket.data,
275 out_creds->ticket.length,
276 cred.tickets.val, &len);
277 if (ret)
278 goto out3;
280 memset (&enc_krb_cred_part, 0, sizeof(enc_krb_cred_part));
281 ALLOC_SEQ(&enc_krb_cred_part.ticket_info, 1);
282 if (enc_krb_cred_part.ticket_info.val == NULL) {
283 ret = ENOMEM;
284 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
285 goto out4;
288 if (auth_context->flags & KRB5_AUTH_CONTEXT_DO_TIME) {
289 krb5_timestamp sec;
290 int32_t usec;
292 krb5_us_timeofday (context, &sec, &usec);
294 ALLOC(enc_krb_cred_part.timestamp, 1);
295 if (enc_krb_cred_part.timestamp == NULL) {
296 ret = ENOMEM;
297 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
298 goto out4;
300 *enc_krb_cred_part.timestamp = sec;
301 ALLOC(enc_krb_cred_part.usec, 1);
302 if (enc_krb_cred_part.usec == NULL) {
303 ret = ENOMEM;
304 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
305 goto out4;
307 *enc_krb_cred_part.usec = usec;
308 } else {
309 enc_krb_cred_part.timestamp = NULL;
310 enc_krb_cred_part.usec = NULL;
313 if (auth_context->local_address && auth_context->local_port && paddrs) {
315 ret = krb5_make_addrport (context,
316 &enc_krb_cred_part.s_address,
317 auth_context->local_address,
318 auth_context->local_port);
319 if (ret)
320 goto out4;
323 if (auth_context->remote_address) {
324 if (auth_context->remote_port) {
325 krb5_boolean noaddr;
326 krb5_const_realm srealm;
328 srealm = krb5_principal_get_realm(context, out_creds->server);
329 /* Is this correct, and should we use the paddrs == NULL
330 trick here as well? Having an address-less ticket may
331 indicate that we don't know our own global address, but
332 it does not necessary mean that we don't know the
333 server's. */
334 krb5_appdefault_boolean(context, NULL, srealm, "no-addresses",
335 FALSE, &noaddr);
336 if (!noaddr) {
337 ret = krb5_make_addrport (context,
338 &enc_krb_cred_part.r_address,
339 auth_context->remote_address,
340 auth_context->remote_port);
341 if (ret)
342 goto out4;
344 } else {
345 ALLOC(enc_krb_cred_part.r_address, 1);
346 if (enc_krb_cred_part.r_address == NULL) {
347 ret = ENOMEM;
348 krb5_set_error_message(context, ret,
349 N_("malloc: out of memory", ""));
350 goto out4;
353 ret = krb5_copy_address (context, auth_context->remote_address,
354 enc_krb_cred_part.r_address);
355 if (ret)
356 goto out4;
360 /* fill ticket_info.val[0] */
362 enc_krb_cred_part.ticket_info.len = 1;
364 krb_cred_info = enc_krb_cred_part.ticket_info.val;
366 copy_EncryptionKey (&out_creds->session, &krb_cred_info->key);
367 ALLOC(krb_cred_info->prealm, 1);
368 copy_Realm (&out_creds->client->realm, krb_cred_info->prealm);
369 ALLOC(krb_cred_info->pname, 1);
370 copy_PrincipalName(&out_creds->client->name, krb_cred_info->pname);
371 ALLOC(krb_cred_info->flags, 1);
372 *krb_cred_info->flags = out_creds->flags.b;
373 ALLOC(krb_cred_info->authtime, 1);
374 *krb_cred_info->authtime = out_creds->times.authtime;
375 ALLOC(krb_cred_info->starttime, 1);
376 *krb_cred_info->starttime = out_creds->times.starttime;
377 ALLOC(krb_cred_info->endtime, 1);
378 *krb_cred_info->endtime = out_creds->times.endtime;
379 ALLOC(krb_cred_info->renew_till, 1);
380 *krb_cred_info->renew_till = out_creds->times.renew_till;
381 ALLOC(krb_cred_info->srealm, 1);
382 copy_Realm (&out_creds->server->realm, krb_cred_info->srealm);
383 ALLOC(krb_cred_info->sname, 1);
384 copy_PrincipalName (&out_creds->server->name, krb_cred_info->sname);
385 ALLOC(krb_cred_info->caddr, 1);
386 copy_HostAddresses (&out_creds->addresses, krb_cred_info->caddr);
388 krb5_free_creds (context, out_creds);
390 /* encode EncKrbCredPart */
392 ASN1_MALLOC_ENCODE(EncKrbCredPart, buf, buf_size,
393 &enc_krb_cred_part, &len, ret);
394 free_EncKrbCredPart (&enc_krb_cred_part);
395 if (ret) {
396 free_KRB_CRED(&cred);
397 return ret;
399 if(buf_size != len)
400 krb5_abortx(context, "internal error in ASN.1 encoder");
403 * Some older of the MIT gssapi library used clear-text tickets
404 * (warped inside AP-REQ encryption), use the krb5_auth_context
405 * flag KRB5_AUTH_CONTEXT_CLEAR_FORWARDED_CRED to support those
406 * tickets. The session key is used otherwise to encrypt the
407 * forwarded ticket.
410 if (auth_context->flags & KRB5_AUTH_CONTEXT_CLEAR_FORWARDED_CRED) {
411 cred.enc_part.etype = ENCTYPE_NULL;
412 cred.enc_part.kvno = NULL;
413 cred.enc_part.cipher.data = buf;
414 cred.enc_part.cipher.length = buf_size;
415 } else {
417 * Here older versions then 0.7.2 of Heimdal used the local or
418 * remote subkey. That is wrong, the session key should be
419 * used. Heimdal 0.7.2 and newer have code to try both in the
420 * receiving end.
423 ret = krb5_crypto_init(context, auth_context->keyblock, 0, &crypto);
424 if (ret) {
425 free(buf);
426 free_KRB_CRED(&cred);
427 return ret;
429 ret = krb5_encrypt_EncryptedData (context,
430 crypto,
431 KRB5_KU_KRB_CRED,
432 buf,
433 len,
435 &cred.enc_part);
436 free(buf);
437 krb5_crypto_destroy(context, crypto);
438 if (ret) {
439 free_KRB_CRED(&cred);
440 return ret;
444 ASN1_MALLOC_ENCODE(KRB_CRED, buf, buf_size, &cred, &len, ret);
445 free_KRB_CRED (&cred);
446 if (ret)
447 return ret;
448 if(buf_size != len)
449 krb5_abortx(context, "internal error in ASN.1 encoder");
450 out_data->length = len;
451 out_data->data = buf;
452 return 0;
453 out4:
454 free_EncKrbCredPart(&enc_krb_cred_part);
455 out3:
456 free_KRB_CRED(&cred);
457 out2:
458 krb5_free_creds (context, out_creds);
459 return ret;