1 /* 2 * Copyright (c) 1997, 1998, 1999 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.21 1999/12/20 00:57:37 assar 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; 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 ret = ENOMEM; 57 goto fail; 58 } 59 addr->val = tmp; 60 for (a = ai; a != NULL; a = a->ai_next) { 61 ret = krb5_sockaddr2address (a->ai_addr, &addr->val[i++]); 62 if (ret) 63 goto fail; 64 } 65 return 0; 66 fail: 67 krb5_free_addresses (context, addr); 68 return ret; 69 } 70 71 /* 72 * 73 */ 74 75 krb5_error_code 76 krb5_fwd_tgt_creds (krb5_context context, 77 krb5_auth_context auth_context, 78 const char *hostname, 79 krb5_principal client, 80 krb5_principal server, 81 krb5_ccache ccache, 82 int forwardable, 83 krb5_data *out_data) 84 { 85 krb5_flags flags = 0; 86 krb5_creds creds; 87 krb5_error_code ret; 88 89 flags |= KDC_OPT_FORWARDED; 90 91 if (forwardable) 92 flags |= KDC_OPT_FORWARDABLE; 93 94 95 memset (&creds, 0, sizeof(creds)); 96 creds.client = client; 97 creds.server = server; 98 99 ret = krb5_get_forwarded_creds (context, 100 auth_context, 101 ccache, 102 flags, 103 hostname, 104 &creds, 105 out_data); 106 return ret; 107 } 108 109 /* 110 * 111 */ 112 113 krb5_error_code 114 krb5_get_forwarded_creds (krb5_context context, 115 krb5_auth_context auth_context, 116 krb5_ccache ccache, 117 krb5_flags flags, 118 const char *hostname, 119 krb5_creds *in_creds, 120 krb5_data *out_data) 121 { 122 krb5_error_code ret; 123 krb5_creds *out_creds; 124 krb5_addresses addrs; 125 KRB_CRED cred; 126 KrbCredInfo *krb_cred_info; 127 EncKrbCredPart enc_krb_cred_part; 128 size_t len; 129 u_char buf[1024]; 130 int32_t sec, usec; 131 krb5_kdc_flags kdc_flags; 132 krb5_crypto crypto; 133 struct addrinfo *ai; 134 135 addrs.len = 0; 136 addrs.val = NULL; 137 138 ret = getaddrinfo (hostname, NULL, NULL, &ai); 139 if (ret) 140 return ret; 141 142 ret = add_addrs (context, &addrs, ai); 143 freeaddrinfo (ai); 144 if (ret) 145 return ret; 146 147 kdc_flags.i = flags; 148 149 ret = krb5_get_kdc_cred (context, 150 ccache, 151 kdc_flags, 152 &addrs, 153 NULL, 154 in_creds, 155 &out_creds); 156 krb5_free_addresses (context, &addrs); 157 if (ret) { 158 return ret; 159 } 160 161 memset (&cred, 0, sizeof(cred)); 162 cred.pvno = 5; 163 cred.msg_type = krb_cred; 164 ALLOC_SEQ(&cred.tickets, 1); 165 if (cred.tickets.val == NULL) { 166 ret = ENOMEM; 167 goto out2; 168 } 169 ret = decode_Ticket(out_creds->ticket.data, 170 out_creds->ticket.length, 171 cred.tickets.val, &len); 172 if (ret) 173 goto out3; 174 175 memset (&enc_krb_cred_part, 0, sizeof(enc_krb_cred_part)); 176 ALLOC_SEQ(&enc_krb_cred_part.ticket_info, 1); 177 if (enc_krb_cred_part.ticket_info.val == NULL) { 178 ret = ENOMEM; 179 goto out4; 180 } 181 182 krb5_us_timeofday (context, &sec, &usec); 183 184 ALLOC(enc_krb_cred_part.timestamp, 1); 185 if (enc_krb_cred_part.timestamp == NULL) { 186 ret = ENOMEM; 187 goto out4; 188 } 189 *enc_krb_cred_part.timestamp = sec; 190 ALLOC(enc_krb_cred_part.usec, 1); 191 if (enc_krb_cred_part.usec == NULL) { 192 ret = ENOMEM; 193 goto out4; 194 } 195 *enc_krb_cred_part.usec = usec; 196 197 ret = krb5_make_addrport (&enc_krb_cred_part.s_address, 198 auth_context->local_address, 199 auth_context->local_port); 200 if (ret) 201 goto out4; 202 203 ALLOC(enc_krb_cred_part.r_address, 1); 204 if (enc_krb_cred_part.r_address == NULL) { 205 ret = ENOMEM; 206 goto out4; 207 } 208 209 ret = krb5_copy_address (context, auth_context->remote_address, 210 enc_krb_cred_part.r_address); 211 if (ret) 212 goto out4; 213 214 /* fill ticket_info.val[0] */ 215 216 enc_krb_cred_part.ticket_info.len = 1; 217 218 krb_cred_info = enc_krb_cred_part.ticket_info.val; 219 220 copy_EncryptionKey (&out_creds->session, &krb_cred_info->key); 221 ALLOC(krb_cred_info->prealm, 1); 222 copy_Realm (&out_creds->client->realm, krb_cred_info->prealm); 223 ALLOC(krb_cred_info->pname, 1); 224 copy_PrincipalName(&out_creds->client->name, krb_cred_info->pname); 225 ALLOC(krb_cred_info->flags, 1); 226 *krb_cred_info->flags = out_creds->flags.b; 227 ALLOC(krb_cred_info->authtime, 1); 228 *krb_cred_info->authtime = out_creds->times.authtime; 229 ALLOC(krb_cred_info->starttime, 1); 230 *krb_cred_info->starttime = out_creds->times.starttime; 231 ALLOC(krb_cred_info->endtime, 1); 232 *krb_cred_info->endtime = out_creds->times.endtime; 233 ALLOC(krb_cred_info->renew_till, 1); 234 *krb_cred_info->renew_till = out_creds->times.renew_till; 235 ALLOC(krb_cred_info->srealm, 1); 236 copy_Realm (&out_creds->server->realm, krb_cred_info->srealm); 237 ALLOC(krb_cred_info->sname, 1); 238 copy_PrincipalName (&out_creds->server->name, krb_cred_info->sname); 239 ALLOC(krb_cred_info->caddr, 1); 240 copy_HostAddresses (&out_creds->addresses, krb_cred_info->caddr); 241 242 krb5_free_creds (context, out_creds); 243 244 /* encode EncKrbCredPart */ 245 246 ret = krb5_encode_EncKrbCredPart (context, 247 buf + sizeof(buf) - 1, sizeof(buf), 248 &enc_krb_cred_part, &len); 249 free_EncKrbCredPart (&enc_krb_cred_part); 250 if (ret) { 251 free_KRB_CRED(&cred); 252 return ret; 253 } 254 255 krb5_crypto_init(context, auth_context->local_subkey, 0, &crypto); 256 ret = krb5_encrypt_EncryptedData (context, 257 crypto, 258 KRB5_KU_KRB_CRED, 259 buf + sizeof(buf) - len, 260 len, 261 0, 262 &cred.enc_part); 263 krb5_crypto_destroy(context, crypto); 264 if (ret) { 265 free_KRB_CRED(&cred); 266 return ret; 267 } 268 269 ret = encode_KRB_CRED (buf + sizeof(buf) - 1, sizeof(buf), 270 &cred, &len); 271 free_KRB_CRED (&cred); 272 if (ret) 273 return ret; 274 out_data->length = len; 275 out_data->data = malloc(len); 276 if (out_data->data == NULL) 277 return ENOMEM; 278 memcpy (out_data->data, buf + sizeof(buf) - len, len); 279 return 0; 280 out4: 281 free_EncKrbCredPart(&enc_krb_cred_part); 282 out3: 283 free_KRB_CRED(&cred); 284 out2: 285 krb5_free_creds (context, out_creds); 286 return ret; 287 } 288