155682Smarkm/* 2233294Sstas * Copyright (c) 1997 - 2004 Kungliga Tekniska H��gskolan 3233294Sstas * (Royal Institute of Technology, Stockholm, Sweden). 4233294Sstas * All rights reserved. 555682Smarkm * 6233294Sstas * Redistribution and use in source and binary forms, with or without 7233294Sstas * modification, are permitted provided that the following conditions 8233294Sstas * are met: 955682Smarkm * 10233294Sstas * 1. Redistributions of source code must retain the above copyright 11233294Sstas * notice, this list of conditions and the following disclaimer. 1255682Smarkm * 13233294Sstas * 2. Redistributions in binary form must reproduce the above copyright 14233294Sstas * notice, this list of conditions and the following disclaimer in the 15233294Sstas * documentation and/or other materials provided with the distribution. 1655682Smarkm * 17233294Sstas * 3. Neither the name of the Institute nor the names of its contributors 18233294Sstas * may be used to endorse or promote products derived from this software 19233294Sstas * without specific prior written permission. 2055682Smarkm * 21233294Sstas * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 22233294Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23233294Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24233294Sstas * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 25233294Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26233294Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27233294Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28233294Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29233294Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30233294Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31233294Sstas * SUCH DAMAGE. 3255682Smarkm */ 3355682Smarkm 34233294Sstas#include "krb5_locl.h" 3555682Smarkm 3655682Smarkmstatic krb5_error_code 3755682Smarkmadd_addrs(krb5_context context, 3855682Smarkm krb5_addresses *addr, 3955682Smarkm struct addrinfo *ai) 4055682Smarkm{ 4155682Smarkm krb5_error_code ret; 42127808Snectar unsigned n, i; 4355682Smarkm void *tmp; 4455682Smarkm struct addrinfo *a; 4555682Smarkm 4655682Smarkm n = 0; 4755682Smarkm for (a = ai; a != NULL; a = a->ai_next) 4855682Smarkm ++n; 4955682Smarkm 50127808Snectar tmp = realloc(addr->val, (addr->len + n) * sizeof(*addr->val)); 51178825Sdfr if (tmp == NULL && (addr->len + n) != 0) { 5255682Smarkm ret = ENOMEM; 53233294Sstas krb5_set_error_message(context, ret, N_("malloc: out of memory", "")); 5455682Smarkm goto fail; 5555682Smarkm } 5655682Smarkm addr->val = tmp; 57127808Snectar for (i = addr->len; i < (addr->len + n); ++i) { 5872445Sassar addr->val[i].addr_type = 0; 5972445Sassar krb5_data_zero(&addr->val[i].address); 6072445Sassar } 61127808Snectar i = addr->len; 6255682Smarkm for (a = ai; a != NULL; a = a->ai_next) { 63127808Snectar krb5_address ad; 64127808Snectar 65127808Snectar ret = krb5_sockaddr2address (context, a->ai_addr, &ad); 66127808Snectar if (ret == 0) { 67127808Snectar if (krb5_address_search(context, &ad, addr)) 68127808Snectar krb5_free_address(context, &ad); 69127808Snectar else 70127808Snectar addr->val[i++] = ad; 71127808Snectar } 7278527Sassar else if (ret == KRB5_PROG_ATYPE_NOSUPP) 73233294Sstas krb5_clear_error_message (context); 7478527Sassar else 7555682Smarkm goto fail; 76127808Snectar addr->len = i; 7755682Smarkm } 7855682Smarkm return 0; 7955682Smarkmfail: 8055682Smarkm krb5_free_addresses (context, addr); 8155682Smarkm return ret; 8255682Smarkm} 8355682Smarkm 84178825Sdfr/** 85178825Sdfr * Forward credentials for client to host hostname , making them 86178825Sdfr * forwardable if forwardable, and returning the blob of data to sent 87178825Sdfr * in out_data. If hostname == NULL, pick it from server. 88178825Sdfr * 89178825Sdfr * @param context A kerberos 5 context. 90178825Sdfr * @param auth_context the auth context with the key to encrypt the out_data. 91178825Sdfr * @param hostname the host to forward the tickets too. 92178825Sdfr * @param client the client to delegate from. 93178825Sdfr * @param server the server to delegate the credential too. 94178825Sdfr * @param ccache credential cache to use. 95178825Sdfr * @param forwardable make the forwarded ticket forwabledable. 96178825Sdfr * @param out_data the resulting credential. 97178825Sdfr * 98178825Sdfr * @return Return an error code or 0. 99178825Sdfr * 100178825Sdfr * @ingroup krb5_credential 10155682Smarkm */ 10255682Smarkm 103233294SstasKRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL 10455682Smarkmkrb5_fwd_tgt_creds (krb5_context context, 10555682Smarkm krb5_auth_context auth_context, 10655682Smarkm const char *hostname, 10755682Smarkm krb5_principal client, 10855682Smarkm krb5_principal server, 10955682Smarkm krb5_ccache ccache, 11055682Smarkm int forwardable, 11155682Smarkm krb5_data *out_data) 11255682Smarkm{ 11355682Smarkm krb5_flags flags = 0; 11455682Smarkm krb5_creds creds; 11555682Smarkm krb5_error_code ret; 11690926Snectar krb5_const_realm client_realm; 11755682Smarkm 11855682Smarkm flags |= KDC_OPT_FORWARDED; 11955682Smarkm 12055682Smarkm if (forwardable) 12155682Smarkm flags |= KDC_OPT_FORWARDABLE; 12255682Smarkm 12390926Snectar if (hostname == NULL && 12490926Snectar krb5_principal_get_type(context, server) == KRB5_NT_SRV_HST) { 12590926Snectar const char *inst = krb5_principal_get_comp_string(context, server, 0); 12690926Snectar const char *host = krb5_principal_get_comp_string(context, server, 1); 12790926Snectar 12890926Snectar if (inst != NULL && 12990926Snectar strcmp(inst, "host") == 0 && 130233294Sstas host != NULL && 13190926Snectar krb5_principal_get_comp_string(context, server, 2) == NULL) 13290926Snectar hostname = host; 13390926Snectar } 13490926Snectar 13590926Snectar client_realm = krb5_principal_get_realm(context, client); 136233294Sstas 13755682Smarkm memset (&creds, 0, sizeof(creds)); 13855682Smarkm creds.client = client; 13955682Smarkm 140233294Sstas ret = krb5_make_principal(context, 141233294Sstas &creds.server, 142233294Sstas client_realm, 143233294Sstas KRB5_TGS_NAME, 144233294Sstas client_realm, 145233294Sstas NULL); 14690926Snectar if (ret) 14790926Snectar return ret; 14890926Snectar 14955682Smarkm ret = krb5_get_forwarded_creds (context, 15055682Smarkm auth_context, 15155682Smarkm ccache, 15255682Smarkm flags, 15355682Smarkm hostname, 15455682Smarkm &creds, 15555682Smarkm out_data); 15655682Smarkm return ret; 15755682Smarkm} 15855682Smarkm 159178825Sdfr/** 160178825Sdfr * Gets tickets forwarded to hostname. If the tickets that are 161178825Sdfr * forwarded are address-less, the forwarded tickets will also be 162178825Sdfr * address-less. 163233294Sstas * 164178825Sdfr * If the ticket have any address, hostname will be used for figure 165178825Sdfr * out the address to forward the ticket too. This since this might 166178825Sdfr * use DNS, its insecure and also doesn't represent configured all 167178825Sdfr * addresses of the host. For example, the host might have two 168178825Sdfr * adresses, one IPv4 and one IPv6 address where the later is not 169178825Sdfr * published in DNS. This IPv6 address might be used communications 170178825Sdfr * and thus the resulting ticket useless. 17155682Smarkm * 172178825Sdfr * @param context A kerberos 5 context. 173178825Sdfr * @param auth_context the auth context with the key to encrypt the out_data. 174178825Sdfr * @param ccache credential cache to use 175178825Sdfr * @param flags the flags to control the resulting ticket flags 176178825Sdfr * @param hostname the host to forward the tickets too. 177178825Sdfr * @param in_creds the in client and server ticket names. The client 178178825Sdfr * and server components forwarded to the remote host. 179178825Sdfr * @param out_data the resulting credential. 180178825Sdfr * 181178825Sdfr * @return Return an error code or 0. 182178825Sdfr * 183178825Sdfr * @ingroup krb5_credential 18455682Smarkm */ 18555682Smarkm 186233294SstasKRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL 18755682Smarkmkrb5_get_forwarded_creds (krb5_context context, 18855682Smarkm krb5_auth_context auth_context, 18955682Smarkm krb5_ccache ccache, 19055682Smarkm krb5_flags flags, 19155682Smarkm const char *hostname, 19255682Smarkm krb5_creds *in_creds, 19355682Smarkm krb5_data *out_data) 19455682Smarkm{ 19555682Smarkm krb5_error_code ret; 19655682Smarkm krb5_creds *out_creds; 197127808Snectar krb5_addresses addrs, *paddrs; 19855682Smarkm KRB_CRED cred; 19955682Smarkm KrbCredInfo *krb_cred_info; 20055682Smarkm EncKrbCredPart enc_krb_cred_part; 20155682Smarkm size_t len; 202103423Snectar unsigned char *buf; 203103423Snectar size_t buf_size; 20455682Smarkm krb5_kdc_flags kdc_flags; 20555682Smarkm krb5_crypto crypto; 20655682Smarkm struct addrinfo *ai; 207127808Snectar krb5_creds *ticket; 20855682Smarkm 209178825Sdfr paddrs = NULL; 21055682Smarkm addrs.len = 0; 21155682Smarkm addrs.val = NULL; 21255682Smarkm 213178825Sdfr ret = krb5_get_credentials(context, 0, ccache, in_creds, &ticket); 214178825Sdfr if(ret == 0) { 215178825Sdfr if (ticket->addresses.len) 216178825Sdfr paddrs = &addrs; 217178825Sdfr krb5_free_creds (context, ticket); 218178825Sdfr } else { 219178825Sdfr krb5_boolean noaddr; 220178825Sdfr krb5_appdefault_boolean(context, NULL, 221233294Sstas krb5_principal_get_realm(context, 222178825Sdfr in_creds->client), 223178825Sdfr "no-addresses", KRB5_ADDRESSLESS_DEFAULT, 224178825Sdfr &noaddr); 225178825Sdfr if (!noaddr) 226178825Sdfr paddrs = &addrs; 227178825Sdfr } 228233294Sstas 229127808Snectar /* 230178825Sdfr * If tickets have addresses, get the address of the remote host. 231127808Snectar */ 232127808Snectar 233127808Snectar if (paddrs != NULL) { 23455682Smarkm 235127808Snectar ret = getaddrinfo (hostname, NULL, NULL, &ai); 236127808Snectar if (ret) { 237233294Sstas krb5_error_code ret2 = krb5_eai_to_heim_errno(ret, errno); 238233294Sstas krb5_set_error_message(context, ret2, 239233294Sstas N_("resolving host %s failed: %s", 240233294Sstas "hostname, error"), 241127808Snectar hostname, gai_strerror(ret)); 242233294Sstas return ret2; 243127808Snectar } 244233294Sstas 245127808Snectar ret = add_addrs (context, &addrs, ai); 246127808Snectar freeaddrinfo (ai); 247127808Snectar if (ret) 248127808Snectar return ret; 249127808Snectar } 250233294Sstas 251178825Sdfr kdc_flags.b = int2KDCOptions(flags); 25255682Smarkm 25355682Smarkm ret = krb5_get_kdc_cred (context, 25455682Smarkm ccache, 25555682Smarkm kdc_flags, 256127808Snectar paddrs, 25755682Smarkm NULL, 25855682Smarkm in_creds, 25955682Smarkm &out_creds); 26055682Smarkm krb5_free_addresses (context, &addrs); 261178825Sdfr if (ret) 26255682Smarkm return ret; 26355682Smarkm 26455682Smarkm memset (&cred, 0, sizeof(cred)); 26555682Smarkm cred.pvno = 5; 26655682Smarkm cred.msg_type = krb_cred; 26755682Smarkm ALLOC_SEQ(&cred.tickets, 1); 26855682Smarkm if (cred.tickets.val == NULL) { 26955682Smarkm ret = ENOMEM; 270233294Sstas krb5_set_error_message(context, ret, N_("malloc: out of memory", "")); 27155682Smarkm goto out2; 27255682Smarkm } 27355682Smarkm ret = decode_Ticket(out_creds->ticket.data, 27455682Smarkm out_creds->ticket.length, 27555682Smarkm cred.tickets.val, &len); 27655682Smarkm if (ret) 27755682Smarkm goto out3; 27855682Smarkm 27955682Smarkm memset (&enc_krb_cred_part, 0, sizeof(enc_krb_cred_part)); 28055682Smarkm ALLOC_SEQ(&enc_krb_cred_part.ticket_info, 1); 28155682Smarkm if (enc_krb_cred_part.ticket_info.val == NULL) { 28255682Smarkm ret = ENOMEM; 283233294Sstas krb5_set_error_message(context, ret, N_("malloc: out of memory", "")); 28455682Smarkm goto out4; 28555682Smarkm } 286233294Sstas 287127808Snectar if (auth_context->flags & KRB5_AUTH_CONTEXT_DO_TIME) { 288178825Sdfr krb5_timestamp sec; 289178825Sdfr int32_t usec; 290233294Sstas 291127808Snectar krb5_us_timeofday (context, &sec, &usec); 292233294Sstas 293127808Snectar ALLOC(enc_krb_cred_part.timestamp, 1); 294127808Snectar if (enc_krb_cred_part.timestamp == NULL) { 295127808Snectar ret = ENOMEM; 296233294Sstas krb5_set_error_message(context, ret, N_("malloc: out of memory", "")); 297127808Snectar goto out4; 298127808Snectar } 299127808Snectar *enc_krb_cred_part.timestamp = sec; 300127808Snectar ALLOC(enc_krb_cred_part.usec, 1); 301127808Snectar if (enc_krb_cred_part.usec == NULL) { 302127808Snectar ret = ENOMEM; 303233294Sstas krb5_set_error_message(context, ret, N_("malloc: out of memory", "")); 304127808Snectar goto out4; 305127808Snectar } 306127808Snectar *enc_krb_cred_part.usec = usec; 307127808Snectar } else { 308127808Snectar enc_krb_cred_part.timestamp = NULL; 309127808Snectar enc_krb_cred_part.usec = NULL; 31055682Smarkm } 31155682Smarkm 312178825Sdfr if (auth_context->local_address && auth_context->local_port && paddrs) { 31390926Snectar 314178825Sdfr ret = krb5_make_addrport (context, 315178825Sdfr &enc_krb_cred_part.s_address, 316178825Sdfr auth_context->local_address, 317178825Sdfr auth_context->local_port); 318178825Sdfr if (ret) 319178825Sdfr goto out4; 32072445Sassar } 32155682Smarkm 32272445Sassar if (auth_context->remote_address) { 323102644Snectar if (auth_context->remote_port) { 324102644Snectar krb5_boolean noaddr; 325178825Sdfr krb5_const_realm srealm; 326102644Snectar 327178825Sdfr srealm = krb5_principal_get_realm(context, out_creds->server); 328178825Sdfr /* Is this correct, and should we use the paddrs == NULL 329178825Sdfr trick here as well? Having an address-less ticket may 330178825Sdfr indicate that we don't know our own global address, but 331178825Sdfr it does not necessary mean that we don't know the 332178825Sdfr server's. */ 333178825Sdfr krb5_appdefault_boolean(context, NULL, srealm, "no-addresses", 334102644Snectar FALSE, &noaddr); 335102644Snectar if (!noaddr) { 336102644Snectar ret = krb5_make_addrport (context, 337102644Snectar &enc_krb_cred_part.r_address, 338102644Snectar auth_context->remote_address, 339102644Snectar auth_context->remote_port); 340102644Snectar if (ret) 341102644Snectar goto out4; 342102644Snectar } 343102644Snectar } else { 344102644Snectar ALLOC(enc_krb_cred_part.r_address, 1); 345102644Snectar if (enc_krb_cred_part.r_address == NULL) { 346102644Snectar ret = ENOMEM; 347233294Sstas krb5_set_error_message(context, ret, 348233294Sstas N_("malloc: out of memory", "")); 349102644Snectar goto out4; 350102644Snectar } 351102644Snectar 352102644Snectar ret = krb5_copy_address (context, auth_context->remote_address, 353102644Snectar enc_krb_cred_part.r_address); 354102644Snectar if (ret) 355102644Snectar goto out4; 35672445Sassar } 35755682Smarkm } 35855682Smarkm 35955682Smarkm /* fill ticket_info.val[0] */ 36055682Smarkm 36155682Smarkm enc_krb_cred_part.ticket_info.len = 1; 36255682Smarkm 36355682Smarkm krb_cred_info = enc_krb_cred_part.ticket_info.val; 36455682Smarkm 36555682Smarkm copy_EncryptionKey (&out_creds->session, &krb_cred_info->key); 36655682Smarkm ALLOC(krb_cred_info->prealm, 1); 36755682Smarkm copy_Realm (&out_creds->client->realm, krb_cred_info->prealm); 36855682Smarkm ALLOC(krb_cred_info->pname, 1); 36955682Smarkm copy_PrincipalName(&out_creds->client->name, krb_cred_info->pname); 37055682Smarkm ALLOC(krb_cred_info->flags, 1); 37155682Smarkm *krb_cred_info->flags = out_creds->flags.b; 37255682Smarkm ALLOC(krb_cred_info->authtime, 1); 37355682Smarkm *krb_cred_info->authtime = out_creds->times.authtime; 37455682Smarkm ALLOC(krb_cred_info->starttime, 1); 37555682Smarkm *krb_cred_info->starttime = out_creds->times.starttime; 37655682Smarkm ALLOC(krb_cred_info->endtime, 1); 37755682Smarkm *krb_cred_info->endtime = out_creds->times.endtime; 37855682Smarkm ALLOC(krb_cred_info->renew_till, 1); 37955682Smarkm *krb_cred_info->renew_till = out_creds->times.renew_till; 38055682Smarkm ALLOC(krb_cred_info->srealm, 1); 38155682Smarkm copy_Realm (&out_creds->server->realm, krb_cred_info->srealm); 38255682Smarkm ALLOC(krb_cred_info->sname, 1); 38355682Smarkm copy_PrincipalName (&out_creds->server->name, krb_cred_info->sname); 38455682Smarkm ALLOC(krb_cred_info->caddr, 1); 38555682Smarkm copy_HostAddresses (&out_creds->addresses, krb_cred_info->caddr); 38655682Smarkm 38755682Smarkm krb5_free_creds (context, out_creds); 38855682Smarkm 38955682Smarkm /* encode EncKrbCredPart */ 39055682Smarkm 391233294Sstas ASN1_MALLOC_ENCODE(EncKrbCredPart, buf, buf_size, 392103423Snectar &enc_krb_cred_part, &len, ret); 39355682Smarkm free_EncKrbCredPart (&enc_krb_cred_part); 39455682Smarkm if (ret) { 39555682Smarkm free_KRB_CRED(&cred); 39655682Smarkm return ret; 397103423Snectar } 398103423Snectar if(buf_size != len) 399103423Snectar krb5_abortx(context, "internal error in ASN.1 encoder"); 40055682Smarkm 401178825Sdfr /** 402178825Sdfr * Some older of the MIT gssapi library used clear-text tickets 403178825Sdfr * (warped inside AP-REQ encryption), use the krb5_auth_context 404178825Sdfr * flag KRB5_AUTH_CONTEXT_CLEAR_FORWARDED_CRED to support those 405178825Sdfr * tickets. The session key is used otherwise to encrypt the 406178825Sdfr * forwarded ticket. 407178825Sdfr */ 408103423Snectar 409178825Sdfr if (auth_context->flags & KRB5_AUTH_CONTEXT_CLEAR_FORWARDED_CRED) { 410178825Sdfr cred.enc_part.etype = ENCTYPE_NULL; 411178825Sdfr cred.enc_part.kvno = NULL; 412178825Sdfr cred.enc_part.cipher.data = buf; 413178825Sdfr cred.enc_part.cipher.length = buf_size; 414178825Sdfr } else { 415233294Sstas /* 416178825Sdfr * Here older versions then 0.7.2 of Heimdal used the local or 417178825Sdfr * remote subkey. That is wrong, the session key should be 418178825Sdfr * used. Heimdal 0.7.2 and newer have code to try both in the 419178825Sdfr * receiving end. 420178825Sdfr */ 421233294Sstas 422178825Sdfr ret = krb5_crypto_init(context, auth_context->keyblock, 0, &crypto); 423178825Sdfr if (ret) { 424178825Sdfr free(buf); 425178825Sdfr free_KRB_CRED(&cred); 426178825Sdfr return ret; 427178825Sdfr } 428178825Sdfr ret = krb5_encrypt_EncryptedData (context, 429178825Sdfr crypto, 430178825Sdfr KRB5_KU_KRB_CRED, 431178825Sdfr buf, 432178825Sdfr len, 433178825Sdfr 0, 434178825Sdfr &cred.enc_part); 435103423Snectar free(buf); 436178825Sdfr krb5_crypto_destroy(context, crypto); 437178825Sdfr if (ret) { 438178825Sdfr free_KRB_CRED(&cred); 439178825Sdfr return ret; 440178825Sdfr } 44172445Sassar } 44255682Smarkm 443103423Snectar ASN1_MALLOC_ENCODE(KRB_CRED, buf, buf_size, &cred, &len, ret); 44455682Smarkm free_KRB_CRED (&cred); 44555682Smarkm if (ret) 44655682Smarkm return ret; 447103423Snectar if(buf_size != len) 448103423Snectar krb5_abortx(context, "internal error in ASN.1 encoder"); 44955682Smarkm out_data->length = len; 450103423Snectar out_data->data = buf; 45155682Smarkm return 0; 452127808Snectar out4: 45355682Smarkm free_EncKrbCredPart(&enc_krb_cred_part); 454127808Snectar out3: 45555682Smarkm free_KRB_CRED(&cred); 456127808Snectar out2: 45755682Smarkm krb5_free_creds (context, out_creds); 45855682Smarkm return ret; 45955682Smarkm} 460