1 /* 2 * Copyright (c) 1997 - 2002 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.34 2002/09/04 16:26:04 joda 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 unsigned char *buf; 166 size_t buf_size; 167 int32_t sec, usec; 168 krb5_kdc_flags kdc_flags; 169 krb5_crypto crypto; 170 struct addrinfo *ai; 171 int save_errno; 172 krb5_keyblock *key; 173 174 addrs.len = 0; 175 addrs.val = NULL; 176 177 ret = getaddrinfo (hostname, NULL, NULL, &ai); 178 if (ret) { 179 save_errno = errno; 180 krb5_set_error_string(context, "resolving %s: %s", 181 hostname, gai_strerror(ret)); 182 return krb5_eai_to_heim_errno(ret, save_errno); 183 } 184 185 ret = add_addrs (context, &addrs, ai); 186 freeaddrinfo (ai); 187 if (ret) 188 return ret; 189 190 kdc_flags.i = flags; 191 192 ret = krb5_get_kdc_cred (context, 193 ccache, 194 kdc_flags, 195 &addrs, 196 NULL, 197 in_creds, 198 &out_creds); 199 krb5_free_addresses (context, &addrs); 200 if (ret) { 201 return ret; 202 } 203 204 memset (&cred, 0, sizeof(cred)); 205 cred.pvno = 5; 206 cred.msg_type = krb_cred; 207 ALLOC_SEQ(&cred.tickets, 1); 208 if (cred.tickets.val == NULL) { 209 ret = ENOMEM; 210 krb5_set_error_string(context, "malloc: out of memory"); 211 goto out2; 212 } 213 ret = decode_Ticket(out_creds->ticket.data, 214 out_creds->ticket.length, 215 cred.tickets.val, &len); 216 if (ret) 217 goto out3; 218 219 memset (&enc_krb_cred_part, 0, sizeof(enc_krb_cred_part)); 220 ALLOC_SEQ(&enc_krb_cred_part.ticket_info, 1); 221 if (enc_krb_cred_part.ticket_info.val == NULL) { 222 ret = ENOMEM; 223 krb5_set_error_string(context, "malloc: out of memory"); 224 goto out4; 225 } 226 227 krb5_us_timeofday (context, &sec, &usec); 228 229 ALLOC(enc_krb_cred_part.timestamp, 1); 230 if (enc_krb_cred_part.timestamp == NULL) { 231 ret = ENOMEM; 232 krb5_set_error_string(context, "malloc: out of memory"); 233 goto out4; 234 } 235 *enc_krb_cred_part.timestamp = sec; 236 ALLOC(enc_krb_cred_part.usec, 1); 237 if (enc_krb_cred_part.usec == NULL) { 238 ret = ENOMEM; 239 krb5_set_error_string(context, "malloc: out of memory"); 240 goto out4; 241 } 242 *enc_krb_cred_part.usec = usec; 243 244 if (auth_context->local_address && auth_context->local_port) { 245 krb5_boolean noaddr; 246 const krb5_realm *realm; 247 248 realm = krb5_princ_realm(context, out_creds->server); 249 krb5_appdefault_boolean(context, NULL, *realm, "no-addresses", FALSE, 250 &noaddr); 251 if (!noaddr) { 252 ret = krb5_make_addrport (context, 253 &enc_krb_cred_part.s_address, 254 auth_context->local_address, 255 auth_context->local_port); 256 if (ret) 257 goto out4; 258 } 259 } 260 261 if (auth_context->remote_address) { 262 if (auth_context->remote_port) { 263 krb5_boolean noaddr; 264 const krb5_realm *realm; 265 266 realm = krb5_princ_realm(context, out_creds->server); 267 krb5_appdefault_boolean(context, NULL, *realm, "no-addresses", 268 FALSE, &noaddr); 269 if (!noaddr) { 270 ret = krb5_make_addrport (context, 271 &enc_krb_cred_part.r_address, 272 auth_context->remote_address, 273 auth_context->remote_port); 274 if (ret) 275 goto out4; 276 } 277 } else { 278 ALLOC(enc_krb_cred_part.r_address, 1); 279 if (enc_krb_cred_part.r_address == NULL) { 280 ret = ENOMEM; 281 krb5_set_error_string(context, "malloc: out of memory"); 282 goto out4; 283 } 284 285 ret = krb5_copy_address (context, auth_context->remote_address, 286 enc_krb_cred_part.r_address); 287 if (ret) 288 goto out4; 289 } 290 } 291 292 /* fill ticket_info.val[0] */ 293 294 enc_krb_cred_part.ticket_info.len = 1; 295 296 krb_cred_info = enc_krb_cred_part.ticket_info.val; 297 298 copy_EncryptionKey (&out_creds->session, &krb_cred_info->key); 299 ALLOC(krb_cred_info->prealm, 1); 300 copy_Realm (&out_creds->client->realm, krb_cred_info->prealm); 301 ALLOC(krb_cred_info->pname, 1); 302 copy_PrincipalName(&out_creds->client->name, krb_cred_info->pname); 303 ALLOC(krb_cred_info->flags, 1); 304 *krb_cred_info->flags = out_creds->flags.b; 305 ALLOC(krb_cred_info->authtime, 1); 306 *krb_cred_info->authtime = out_creds->times.authtime; 307 ALLOC(krb_cred_info->starttime, 1); 308 *krb_cred_info->starttime = out_creds->times.starttime; 309 ALLOC(krb_cred_info->endtime, 1); 310 *krb_cred_info->endtime = out_creds->times.endtime; 311 ALLOC(krb_cred_info->renew_till, 1); 312 *krb_cred_info->renew_till = out_creds->times.renew_till; 313 ALLOC(krb_cred_info->srealm, 1); 314 copy_Realm (&out_creds->server->realm, krb_cred_info->srealm); 315 ALLOC(krb_cred_info->sname, 1); 316 copy_PrincipalName (&out_creds->server->name, krb_cred_info->sname); 317 ALLOC(krb_cred_info->caddr, 1); 318 copy_HostAddresses (&out_creds->addresses, krb_cred_info->caddr); 319 320 krb5_free_creds (context, out_creds); 321 322 /* encode EncKrbCredPart */ 323 324 ASN1_MALLOC_ENCODE(EncKrbCredPart, buf, buf_size, 325 &enc_krb_cred_part, &len, ret); 326 free_EncKrbCredPart (&enc_krb_cred_part); 327 if (ret) { 328 free_KRB_CRED(&cred); 329 return ret; 330 } 331 if(buf_size != len) 332 krb5_abortx(context, "internal error in ASN.1 encoder"); 333 334 if (auth_context->local_subkey) 335 key = auth_context->local_subkey; 336 else if (auth_context->remote_subkey) 337 key = auth_context->remote_subkey; 338 else 339 key = auth_context->keyblock; 340 341 ret = krb5_crypto_init(context, key, 0, &crypto); 342 if (ret) { 343 free(buf); 344 free_KRB_CRED(&cred); 345 return ret; 346 } 347 ret = krb5_encrypt_EncryptedData (context, 348 crypto, 349 KRB5_KU_KRB_CRED, 350 buf, 351 len, 352 0, 353 &cred.enc_part); 354 free(buf); 355 krb5_crypto_destroy(context, crypto); 356 if (ret) { 357 free_KRB_CRED(&cred); 358 return ret; 359 } 360 361 ASN1_MALLOC_ENCODE(KRB_CRED, buf, buf_size, &cred, &len, ret); 362 free_KRB_CRED (&cred); 363 if (ret) 364 return ret; 365 if(buf_size != len) 366 krb5_abortx(context, "internal error in ASN.1 encoder"); 367 out_data->length = len; 368 out_data->data = buf; 369 return 0; 370 out4: 371 free_EncKrbCredPart(&enc_krb_cred_part); 372 out3: 373 free_KRB_CRED(&cred); 374 out2: 375 krb5_free_creds (context, out_creds); 376 return ret; 377 } 378