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