| 1 |
|
| 2 |
/* |
| 3 |
* Copyright (c) 1997 - 2007 Kungliga Tekniska Högskolan |
| 4 |
* (Royal Institute of Technology, Stockholm, Sweden). |
| 5 |
* All rights reserved. |
| 6 |
* |
| 7 |
* Redistribution and use in source and binary forms, with or without |
| 8 |
* modification, are permitted provided that the following conditions |
| 9 |
* are met: |
| 10 |
* |
| 11 |
* 1. Redistributions of source code must retain the above copyright |
| 12 |
* notice, this list of conditions and the following disclaimer. |
| 13 |
* |
| 14 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 15 |
* notice, this list of conditions and the following disclaimer in the |
| 16 |
* documentation and/or other materials provided with the distribution. |
| 17 |
* |
| 18 |
* 3. Neither the name of the Institute nor the names of its contributors |
| 19 |
* may be used to endorse or promote products derived from this software |
| 20 |
* without specific prior written permission. |
| 21 |
* |
| 22 |
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND |
| 23 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 24 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 25 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE |
| 26 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 27 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 28 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 29 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 30 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 31 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 32 |
* SUCH DAMAGE. |
| 33 |
*/ |
| 34 |
|
| 35 |
#include "krb5_locl.h" |
| 36 |
|
| 37 |
static krb5_error_code |
| 38 |
decrypt_tkt_enc_part (krb5_context context, |
| 39 |
krb5_keyblock *key, |
| 40 |
EncryptedData *enc_part, |
| 41 |
EncTicketPart *decr_part) |
| 42 |
{ |
| 43 |
krb5_error_code ret; |
| 44 |
krb5_data plain; |
| 45 |
size_t len; |
| 46 |
krb5_crypto crypto; |
| 47 |
|
| 48 |
ret = krb5_crypto_init(context, key, 0, &crypto); |
| 49 |
if (ret) |
| 50 |
return ret; |
| 51 |
ret = krb5_decrypt_EncryptedData (context, |
| 52 |
crypto, |
| 53 |
KRB5_KU_TICKET, |
| 54 |
enc_part, |
| 55 |
&plain); |
| 56 |
krb5_crypto_destroy(context, crypto); |
| 57 |
if (ret) |
| 58 |
return ret; |
| 59 |
|
| 60 |
ret = decode_EncTicketPart(plain.data, plain.length, decr_part, &len); |
| 61 |
if (ret) |
| 62 |
krb5_set_error_message(context, ret, |
| 63 |
N_("Failed to decode encrypted " |
| 64 |
"ticket part", "")); |
| 65 |
krb5_data_free (&plain); |
| 66 |
return ret; |
| 67 |
} |
| 68 |
|
| 69 |
static krb5_error_code |
| 70 |
decrypt_authenticator (krb5_context context, |
| 71 |
EncryptionKey *key, |
| 72 |
EncryptedData *enc_part, |
| 73 |
Authenticator *authenticator, |
| 74 |
krb5_key_usage usage) |
| 75 |
{ |
| 76 |
krb5_error_code ret; |
| 77 |
krb5_data plain; |
| 78 |
size_t len; |
| 79 |
krb5_crypto crypto; |
| 80 |
|
| 81 |
ret = krb5_crypto_init(context, key, 0, &crypto); |
| 82 |
if (ret) |
| 83 |
return ret; |
| 84 |
ret = krb5_decrypt_EncryptedData (context, |
| 85 |
crypto, |
| 86 |
usage /* KRB5_KU_AP_REQ_AUTH */, |
| 87 |
enc_part, |
| 88 |
&plain); |
| 89 |
/* for backwards compatibility, also try the old usage */ |
| 90 |
if (ret && usage == KRB5_KU_TGS_REQ_AUTH) |
| 91 |
ret = krb5_decrypt_EncryptedData (context, |
| 92 |
crypto, |
| 93 |
KRB5_KU_AP_REQ_AUTH, |
| 94 |
enc_part, |
| 95 |
&plain); |
| 96 |
krb5_crypto_destroy(context, crypto); |
| 97 |
if (ret) |
| 98 |
return ret; |
| 99 |
|
| 100 |
ret = decode_Authenticator(plain.data, plain.length, |
| 101 |
authenticator, &len); |
| 102 |
krb5_data_free (&plain); |
| 103 |
return ret; |
| 104 |
} |
| 105 |
|
| 106 |
KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL |
| 107 |
krb5_decode_ap_req(krb5_context context, |
| 108 |
const krb5_data *inbuf, |
| 109 |
krb5_ap_req *ap_req) |
| 110 |
{ |
| 111 |
krb5_error_code ret; |
| 112 |
size_t len; |
| 113 |
ret = decode_AP_REQ(inbuf->data, inbuf->length, ap_req, &len); |
| 114 |
if (ret) |
| 115 |
return ret; |
| 116 |
if (ap_req->pvno != 5){ |
| 117 |
free_AP_REQ(ap_req); |
| 118 |
krb5_clear_error_message (context); |
| 119 |
return KRB5KRB_AP_ERR_BADVERSION; |
| 120 |
} |
| 121 |
if (ap_req->msg_type != krb_ap_req){ |
| 122 |
free_AP_REQ(ap_req); |
| 123 |
krb5_clear_error_message (context); |
| 124 |
return KRB5KRB_AP_ERR_MSG_TYPE; |
| 125 |
} |
| 126 |
if (ap_req->ticket.tkt_vno != 5){ |
| 127 |
free_AP_REQ(ap_req); |
| 128 |
krb5_clear_error_message (context); |
| 129 |
return KRB5KRB_AP_ERR_BADVERSION; |
| 130 |
} |
| 131 |
return 0; |
| 132 |
} |
| 133 |
|
| 134 |
static krb5_error_code |
| 135 |
check_transited(krb5_context context, Ticket *ticket, EncTicketPart *enc) |
| 136 |
{ |
| 137 |
char **realms; |
| 138 |
unsigned int num_realms, n; |
| 139 |
krb5_error_code ret; |
| 140 |
|
| 141 |
/* |
| 142 |
* Windows 2000 and 2003 uses this inside their TGT so it's normaly |
| 143 |
* not seen by others, however, samba4 joined with a Windows AD as |
| 144 |
* a Domain Controller gets exposed to this. |
| 145 |
*/ |
| 146 |
if(enc->transited.tr_type == 0 && enc->transited.contents.length == 0) |
| 147 |
return 0; |
| 148 |
|
| 149 |
if(enc->transited.tr_type != DOMAIN_X500_COMPRESS) |
| 150 |
return KRB5KDC_ERR_TRTYPE_NOSUPP; |
| 151 |
|
| 152 |
if(enc->transited.contents.length == 0) |
| 153 |
return 0; |
| 154 |
|
| 155 |
ret = krb5_domain_x500_decode(context, enc->transited.contents, |
| 156 |
&realms, &num_realms, |
| 157 |
enc->crealm, |
| 158 |
ticket->realm); |
| 159 |
if(ret) |
| 160 |
return ret; |
| 161 |
ret = krb5_check_transited(context, enc->crealm, |
| 162 |
ticket->realm, |
| 163 |
realms, num_realms, NULL); |
| 164 |
for (n = 0; n < num_realms; n++) |
| 165 |
free(realms[n]); |
| 166 |
free(realms); |
| 167 |
return ret; |
| 168 |
} |
| 169 |
|
| 170 |
static krb5_error_code |
| 171 |
find_etypelist(krb5_context context, |
| 172 |
krb5_auth_context auth_context, |
| 173 |
EtypeList *etypes) |
| 174 |
{ |
| 175 |
krb5_error_code ret; |
| 176 |
krb5_data data; |
| 177 |
|
| 178 |
ret = _krb5_get_ad(context, auth_context->authenticator->authorization_data, NULL, KRB5_AUTHDATA_GSS_API_ETYPE_NEGOTIATION, &data); |
| 179 |
if (ret) |
| 180 |
return 0; |
| 181 |
|
| 182 |
ret = decode_EtypeList(data.data, data.length, etypes, NULL); |
| 183 |
krb5_data_free(&data); |
| 184 |
if (ret) |
| 185 |
krb5_clear_error_message(context); |
| 186 |
|
| 187 |
return ret; |
| 188 |
} |
| 189 |
|
| 190 |
KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL |
| 191 |
krb5_decrypt_ticket(krb5_context context, |
| 192 |
Ticket *ticket, |
| 193 |
krb5_keyblock *key, |
| 194 |
EncTicketPart *out, |
| 195 |
krb5_flags flags) |
| 196 |
{ |
| 197 |
EncTicketPart t; |
| 198 |
krb5_error_code ret; |
| 199 |
ret = decrypt_tkt_enc_part (context, key, &ticket->enc_part, &t); |
| 200 |
if (ret) |
| 201 |
return ret; |
| 202 |
|
| 203 |
{ |
| 204 |
krb5_timestamp now; |
| 205 |
time_t start = t.authtime; |
| 206 |
|
| 207 |
krb5_timeofday (context, &now); |
| 208 |
if(t.starttime) |
| 209 |
start = *t.starttime; |
| 210 |
if(start - now > context->max_skew |
| 211 |
|| (t.flags.invalid |
| 212 |
&& !(flags & KRB5_VERIFY_AP_REQ_IGNORE_INVALID))) { |
| 213 |
free_EncTicketPart(&t); |
| 214 |
krb5_clear_error_message (context); |
| 215 |
return KRB5KRB_AP_ERR_TKT_NYV; |
| 216 |
} |
| 217 |
if(now - t.endtime > context->max_skew) { |
| 218 |
free_EncTicketPart(&t); |
| 219 |
krb5_clear_error_message (context); |
| 220 |
return KRB5KRB_AP_ERR_TKT_EXPIRED; |
| 221 |
} |
| 222 |
|
| 223 |
if(!t.flags.transited_policy_checked) { |
| 224 |
ret = check_transited(context, ticket, &t); |
| 225 |
if(ret) { |
| 226 |
free_EncTicketPart(&t); |
| 227 |
return ret; |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
if(out) |
| 233 |
*out = t; |
| 234 |
else |
| 235 |
free_EncTicketPart(&t); |
| 236 |
return 0; |
| 237 |
} |
| 238 |
|
| 239 |
KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL |
| 240 |
krb5_verify_authenticator_checksum(krb5_context context, |
| 241 |
krb5_auth_context ac, |
| 242 |
void *data, |
| 243 |
size_t len) |
| 244 |
{ |
| 245 |
krb5_error_code ret; |
| 246 |
krb5_keyblock *key = NULL; |
| 247 |
krb5_authenticator authenticator; |
| 248 |
krb5_crypto crypto; |
| 249 |
|
| 250 |
ret = krb5_auth_con_getauthenticator(context, ac, &authenticator); |
| 251 |
if (ret) |
| 252 |
return ret; |
| 253 |
if (authenticator->cksum == NULL) { |
| 254 |
ret = -17; |
| 255 |
goto out; |
| 256 |
} |
| 257 |
ret = krb5_auth_con_getkey(context, ac, &key); |
| 258 |
if (ret) |
| 259 |
goto out; |
| 260 |
ret = krb5_crypto_init(context, key, 0, &crypto); |
| 261 |
if (ret) |
| 262 |
goto out; |
| 263 |
ret = krb5_verify_checksum(context, crypto, |
| 264 |
KRB5_KU_AP_REQ_AUTH_CKSUM, |
| 265 |
data, len, authenticator->cksum); |
| 266 |
krb5_crypto_destroy(context, crypto); |
| 267 |
out: |
| 268 |
krb5_free_authenticator(context, &authenticator); |
| 269 |
krb5_free_keyblock(context, key); |
| 270 |
return ret; |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL |
| 275 |
krb5_verify_ap_req(krb5_context context, |
| 276 |
krb5_auth_context *auth_context, |
| 277 |
krb5_ap_req *ap_req, |
| 278 |
krb5_const_principal server, |
| 279 |
krb5_keyblock *keyblock, |
| 280 |
krb5_flags flags, |
| 281 |
krb5_flags *ap_req_options, |
| 282 |
krb5_ticket **ticket) |
| 283 |
{ |
| 284 |
return krb5_verify_ap_req2 (context, |
| 285 |
auth_context, |
| 286 |
ap_req, |
| 287 |
server, |
| 288 |
keyblock, |
| 289 |
flags, |
| 290 |
ap_req_options, |
| 291 |
ticket, |
| 292 |
KRB5_KU_AP_REQ_AUTH); |
| 293 |
} |
| 294 |
|
| 295 |
KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL |
| 296 |
krb5_verify_ap_req2(krb5_context context, |
| 297 |
krb5_auth_context *auth_context, |
| 298 |
krb5_ap_req *ap_req, |
| 299 |
krb5_const_principal server, |
| 300 |
krb5_keyblock *keyblock, |
| 301 |
krb5_flags flags, |
| 302 |
krb5_flags *ap_req_options, |
| 303 |
krb5_ticket **ticket, |
| 304 |
krb5_key_usage usage) |
| 305 |
{ |
| 306 |
krb5_ticket *t; |
| 307 |
krb5_auth_context ac; |
| 308 |
krb5_error_code ret; |
| 309 |
EtypeList etypes; |
| 310 |
|
| 311 |
memset(&etypes, 0, sizeof(etypes)); |
| 312 |
|
| 313 |
if (ticket) |
| 314 |
*ticket = NULL; |
| 315 |
|
| 316 |
if (auth_context && *auth_context) { |
| 317 |
ac = *auth_context; |
| 318 |
} else { |
| 319 |
ret = krb5_auth_con_init (context, &ac); |
| 320 |
if (ret) |
| 321 |
return ret; |
| 322 |
} |
| 323 |
|
| 324 |
t = calloc(1, sizeof(*t)); |
| 325 |
if (t == NULL) { |
| 326 |
ret = krb5_enomem(context); |
| 327 |
goto out; |
| 328 |
} |
| 329 |
|
| 330 |
if (ap_req->ap_options.use_session_key && ac->keyblock){ |
| 331 |
ret = krb5_decrypt_ticket(context, &ap_req->ticket, |
| 332 |
ac->keyblock, |
| 333 |
&t->ticket, |
| 334 |
flags); |
| 335 |
krb5_free_keyblock(context, ac->keyblock); |
| 336 |
ac->keyblock = NULL; |
| 337 |
}else |
| 338 |
ret = krb5_decrypt_ticket(context, &ap_req->ticket, |
| 339 |
keyblock, |
| 340 |
&t->ticket, |
| 341 |
flags); |
| 342 |
|
| 343 |
if(ret) |
| 344 |
goto out; |
| 345 |
|
| 346 |
ret = _krb5_principalname2krb5_principal(context, |
| 347 |
&t->server, |
| 348 |
ap_req->ticket.sname, |
| 349 |
ap_req->ticket.realm); |
| 350 |
if (ret) goto out; |
| 351 |
ret = _krb5_principalname2krb5_principal(context, |
| 352 |
&t->client, |
| 353 |
t->ticket.cname, |
| 354 |
t->ticket.crealm); |
| 355 |
if (ret) goto out; |
| 356 |
|
| 357 |
ret = decrypt_authenticator (context, |
| 358 |
&t->ticket.key, |
| 359 |
&ap_req->authenticator, |
| 360 |
ac->authenticator, |
| 361 |
usage); |
| 362 |
if (ret) |
| 363 |
goto out; |
| 364 |
|
| 365 |
{ |
| 366 |
krb5_principal p1, p2; |
| 367 |
krb5_boolean res; |
| 368 |
|
| 369 |
_krb5_principalname2krb5_principal(context, |
| 370 |
&p1, |
| 371 |
ac->authenticator->cname, |
| 372 |
ac->authenticator->crealm); |
| 373 |
_krb5_principalname2krb5_principal(context, |
| 374 |
&p2, |
| 375 |
t->ticket.cname, |
| 376 |
t->ticket.crealm); |
| 377 |
res = krb5_principal_compare (context, p1, p2); |
| 378 |
krb5_free_principal (context, p1); |
| 379 |
krb5_free_principal (context, p2); |
| 380 |
if (!res) { |
| 381 |
ret = KRB5KRB_AP_ERR_BADMATCH; |
| 382 |
krb5_clear_error_message (context); |
| 383 |
goto out; |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
/* check addresses */ |
| 388 |
|
| 389 |
if (t->ticket.caddr |
| 390 |
&& ac->remote_address |
| 391 |
&& !krb5_address_search (context, |
| 392 |
ac->remote_address, |
| 393 |
t->ticket.caddr)) { |
| 394 |
ret = KRB5KRB_AP_ERR_BADADDR; |
| 395 |
krb5_clear_error_message (context); |
| 396 |
goto out; |
| 397 |
} |
| 398 |
|
| 399 |
/* check timestamp in authenticator */ |
| 400 |
{ |
| 401 |
krb5_timestamp now; |
| 402 |
|
| 403 |
krb5_timeofday (context, &now); |
| 404 |
|
| 405 |
if (labs(ac->authenticator->ctime - now) > context->max_skew) { |
| 406 |
ret = KRB5KRB_AP_ERR_SKEW; |
| 407 |
krb5_clear_error_message (context); |
| 408 |
goto out; |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
if (ac->authenticator->seq_number) |
| 413 |
krb5_auth_con_setremoteseqnumber(context, ac, |
| 414 |
*ac->authenticator->seq_number); |
| 415 |
|
| 416 |
/* XXX - Xor sequence numbers */ |
| 417 |
|
| 418 |
if (ac->authenticator->subkey) { |
| 419 |
ret = krb5_auth_con_setremotesubkey(context, ac, |
| 420 |
ac->authenticator->subkey); |
| 421 |
if (ret) |
| 422 |
goto out; |
| 423 |
} |
| 424 |
|
| 425 |
ret = find_etypelist(context, ac, &etypes); |
| 426 |
if (ret) |
| 427 |
goto out; |
| 428 |
|
| 429 |
ac->keytype = ETYPE_NULL; |
| 430 |
|
| 431 |
if (etypes.val) { |
| 432 |
size_t i; |
| 433 |
|
| 434 |
for (i = 0; i < etypes.len; i++) { |
| 435 |
if (krb5_enctype_valid(context, etypes.val[i]) == 0) { |
| 436 |
ac->keytype = etypes.val[i]; |
| 437 |
break; |
| 438 |
} |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
/* save key */ |
| 443 |
ret = krb5_copy_keyblock(context, &t->ticket.key, &ac->keyblock); |
| 444 |
if (ret) goto out; |
| 445 |
|
| 446 |
if (ap_req_options) { |
| 447 |
*ap_req_options = 0; |
| 448 |
if (ac->keytype != (krb5_enctype)ETYPE_NULL) |
| 449 |
*ap_req_options |= AP_OPTS_USE_SUBKEY; |
| 450 |
if (ap_req->ap_options.use_session_key) |
| 451 |
*ap_req_options |= AP_OPTS_USE_SESSION_KEY; |
| 452 |
if (ap_req->ap_options.mutual_required) |
| 453 |
*ap_req_options |= AP_OPTS_MUTUAL_REQUIRED; |
| 454 |
} |
| 455 |
|
| 456 |
if(ticket) |
| 457 |
*ticket = t; |
| 458 |
else |
| 459 |
krb5_free_ticket (context, t); |
| 460 |
if (auth_context) { |
| 461 |
if (*auth_context == NULL) |
| 462 |
*auth_context = ac; |
| 463 |
} else |
| 464 |
krb5_auth_con_free (context, ac); |
| 465 |
free_EtypeList(&etypes); |
| 466 |
return 0; |
| 467 |
out: |
| 468 |
free_EtypeList(&etypes); |
| 469 |
if (t) |
| 470 |
krb5_free_ticket (context, t); |
| 471 |
if (auth_context == NULL || *auth_context == NULL) |
| 472 |
krb5_auth_con_free (context, ac); |
| 473 |
return ret; |
| 474 |
} |
| 475 |
|
| 476 |
/* |
| 477 |
* |
| 478 |
*/ |
| 479 |
|
| 480 |
struct krb5_rd_req_in_ctx_data { |
| 481 |
krb5_keytab keytab; |
| 482 |
krb5_keyblock *keyblock; |
| 483 |
krb5_boolean check_pac; |
| 484 |
}; |
| 485 |
|
| 486 |
struct krb5_rd_req_out_ctx_data { |
| 487 |
krb5_keyblock *keyblock; |
| 488 |
krb5_flags ap_req_options; |
| 489 |
krb5_ticket *ticket; |
| 490 |
krb5_principal server; |
| 491 |
}; |
| 492 |
|
| 493 |
/** |
| 494 |
* Allocate a krb5_rd_req_in_ctx as an input parameter to |
| 495 |
* krb5_rd_req_ctx(). The caller should free the context with |
| 496 |
* krb5_rd_req_in_ctx_free() when done with the context. |
| 497 |
* |
| 498 |
* @param context Keberos 5 context. |
| 499 |
* @param ctx in ctx to krb5_rd_req_ctx(). |
| 500 |
* |
| 501 |
* @return Kerberos 5 error code, see krb5_get_error_message(). |
| 502 |
* |
| 503 |
* @ingroup krb5_auth |
| 504 |
*/ |
| 505 |
|
| 506 |
KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL |
| 507 |
krb5_rd_req_in_ctx_alloc(krb5_context context, krb5_rd_req_in_ctx *ctx) |
| 508 |
{ |
| 509 |
*ctx = calloc(1, sizeof(**ctx)); |
| 510 |
if (*ctx == NULL) |
| 511 |
return krb5_enomem(context); |
| 512 |
(*ctx)->check_pac = (context->flags & KRB5_CTX_F_CHECK_PAC) ? 1 : 0; |
| 513 |
return 0; |
| 514 |
} |
| 515 |
|
| 516 |
/** |
| 517 |
* Set the keytab that krb5_rd_req_ctx() will use. |
| 518 |
* |
| 519 |
* @param context Keberos 5 context. |
| 520 |
* @param in in ctx to krb5_rd_req_ctx(). |
| 521 |
* @param keytab keytab that krb5_rd_req_ctx() will use, only copy the |
| 522 |
* pointer, so the caller must free they keytab after |
| 523 |
* krb5_rd_req_in_ctx_free() is called. |
| 524 |
* |
| 525 |
* @return Kerberos 5 error code, see krb5_get_error_message(). |
| 526 |
* |
| 527 |
* @ingroup krb5_auth |
| 528 |
*/ |
| 529 |
|
| 530 |
KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL |
| 531 |
krb5_rd_req_in_set_keytab(krb5_context context, |
| 532 |
krb5_rd_req_in_ctx in, |
| 533 |
krb5_keytab keytab) |
| 534 |
{ |
| 535 |
in->keytab = keytab; |
| 536 |
return 0; |
| 537 |
} |
| 538 |
|
| 539 |
/** |
| 540 |
* Set if krb5_rq_red() is going to check the Windows PAC or not |
| 541 |
* |
| 542 |
* @param context Keberos 5 context. |
| 543 |
* @param in krb5_rd_req_in_ctx to check the option on. |
| 544 |
* @param flag flag to select if to check the pac (TRUE) or not (FALSE). |
| 545 |
* |
| 546 |
* @return Kerberos 5 error code, see krb5_get_error_message(). |
| 547 |
* |
| 548 |
* @ingroup krb5_auth |
| 549 |
*/ |
| 550 |
|
| 551 |
KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL |
| 552 |
krb5_rd_req_in_set_pac_check(krb5_context context, |
| 553 |
krb5_rd_req_in_ctx in, |
| 554 |
krb5_boolean flag) |
| 555 |
{ |
| 556 |
in->check_pac = flag; |
| 557 |
return 0; |
| 558 |
} |
| 559 |
|
| 560 |
|
| 561 |
KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL |
| 562 |
krb5_rd_req_in_set_keyblock(krb5_context context, |
| 563 |
krb5_rd_req_in_ctx in, |
| 564 |
krb5_keyblock *keyblock) |
| 565 |
{ |
| 566 |
in->keyblock = keyblock; /* XXX should make copy */ |
| 567 |
return 0; |
| 568 |
} |
| 569 |
|
| 570 |
KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL |
| 571 |
krb5_rd_req_out_get_ap_req_options(krb5_context context, |
| 572 |
krb5_rd_req_out_ctx out, |
| 573 |
krb5_flags *ap_req_options) |
| 574 |
{ |
| 575 |
*ap_req_options = out->ap_req_options; |
| 576 |
return 0; |
| 577 |
} |
| 578 |
|
| 579 |
KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL |
| 580 |
krb5_rd_req_out_get_ticket(krb5_context context, |
| 581 |
krb5_rd_req_out_ctx out, |
| 582 |
krb5_ticket **ticket) |
| 583 |
{ |
| 584 |
return krb5_copy_ticket(context, out->ticket, ticket); |
| 585 |
} |
| 586 |
|
| 587 |
KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL |
| 588 |
krb5_rd_req_out_get_keyblock(krb5_context context, |
| 589 |
krb5_rd_req_out_ctx out, |
| 590 |
krb5_keyblock **keyblock) |
| 591 |
{ |
| 592 |
return krb5_copy_keyblock(context, out->keyblock, keyblock); |
| 593 |
} |
| 594 |
|
| 595 |
/** |
| 596 |
* Get the principal that was used in the request from the |
| 597 |
* client. Might not match whats in the ticket if krb5_rd_req_ctx() |
| 598 |
* searched in the keytab for a matching key. |
| 599 |
* |
| 600 |
* @param context a Kerberos 5 context. |
| 601 |
* @param out a krb5_rd_req_out_ctx from krb5_rd_req_ctx(). |
| 602 |
* @param principal return principal, free with krb5_free_principal(). |
| 603 |
* |
| 604 |
* @ingroup krb5_auth |
| 605 |
*/ |
| 606 |
|
| 607 |
KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL |
| 608 |
krb5_rd_req_out_get_server(krb5_context context, |
| 609 |
krb5_rd_req_out_ctx out, |
| 610 |
krb5_principal *principal) |
| 611 |
{ |
| 612 |
return krb5_copy_principal(context, out->server, principal); |
| 613 |
} |
| 614 |
|
| 615 |
KRB5_LIB_FUNCTION void KRB5_LIB_CALL |
| 616 |
krb5_rd_req_in_ctx_free(krb5_context context, krb5_rd_req_in_ctx ctx) |
| 617 |
{ |
| 618 |
free(ctx); |
| 619 |
} |
| 620 |
|
| 621 |
/** |
| 622 |
* Free the krb5_rd_req_out_ctx. |
| 623 |
* |
| 624 |
* @param context Keberos 5 context. |
| 625 |
* @param ctx krb5_rd_req_out_ctx context to free. |
| 626 |
* |
| 627 |
* @ingroup krb5_auth |
| 628 |
*/ |
| 629 |
|
| 630 |
KRB5_LIB_FUNCTION void KRB5_LIB_CALL |
| 631 |
krb5_rd_req_out_ctx_free(krb5_context context, krb5_rd_req_out_ctx ctx) |
| 632 |
{ |
| 633 |
if (ctx->ticket) |
| 634 |
krb5_free_ticket(context, ctx->ticket); |
| 635 |
if (ctx->keyblock) |
| 636 |
krb5_free_keyblock(context, ctx->keyblock); |
| 637 |
if (ctx->server) |
| 638 |
krb5_free_principal(context, ctx->server); |
| 639 |
free(ctx); |
| 640 |
} |
| 641 |
|
| 642 |
/** |
| 643 |
* Process an AP_REQ message. |
| 644 |
* |
| 645 |
* @param context Kerberos 5 context. |
| 646 |
* @param auth_context authentication context of the peer. |
| 647 |
* @param inbuf the AP_REQ message, obtained for example with krb5_read_message(). |
| 648 |
* @param server server principal. |
| 649 |
* @param keytab server keytab. |
| 650 |
* @param ap_req_options set to the AP_REQ options. See the AP_OPTS_* defines. |
| 651 |
* @param ticket on success, set to the authenticated client credentials. |
| 652 |
* Must be deallocated with krb5_free_ticket(). If not |
| 653 |
* interested, pass a NULL value. |
| 654 |
* |
| 655 |
* @return 0 to indicate success. Otherwise a Kerberos error code is |
| 656 |
* returned, see krb5_get_error_message(). |
| 657 |
*/ |
| 658 |
KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL |
| 659 |
krb5_rd_req(krb5_context context, |
| 660 |
krb5_auth_context *auth_context, |
| 661 |
const krb5_data *inbuf, |
| 662 |
krb5_const_principal server, |
| 663 |
krb5_keytab keytab, |
| 664 |
krb5_flags *ap_req_options, |
| 665 |
krb5_ticket **ticket) |
| 666 |
{ |
| 667 |
krb5_error_code ret; |
| 668 |
krb5_rd_req_in_ctx in; |
| 669 |
krb5_rd_req_out_ctx out; |
| 670 |
|
| 671 |
ret = krb5_rd_req_in_ctx_alloc(context, &in); |
| 672 |
if (ret) |
| 673 |
return ret; |
| 674 |
|
| 675 |
ret = krb5_rd_req_in_set_keytab(context, in, keytab); |
| 676 |
if (ret) { |
| 677 |
krb5_rd_req_in_ctx_free(context, in); |
| 678 |
return ret; |
| 679 |
} |
| 680 |
|
| 681 |
ret = krb5_rd_req_ctx(context, auth_context, inbuf, server, in, &out); |
| 682 |
krb5_rd_req_in_ctx_free(context, in); |
| 683 |
if (ret) |
| 684 |
return ret; |
| 685 |
|
| 686 |
if (ap_req_options) |
| 687 |
*ap_req_options = out->ap_req_options; |
| 688 |
if (ticket) { |
| 689 |
ret = krb5_copy_ticket(context, out->ticket, ticket); |
| 690 |
if (ret) |
| 691 |
goto out; |
| 692 |
} |
| 693 |
|
| 694 |
out: |
| 695 |
krb5_rd_req_out_ctx_free(context, out); |
| 696 |
return ret; |
| 697 |
} |
| 698 |
|
| 699 |
/* |
| 700 |
* |
| 701 |
*/ |
| 702 |
|
| 703 |
KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL |
| 704 |
krb5_rd_req_with_keyblock(krb5_context context, |
| 705 |
krb5_auth_context *auth_context, |
| 706 |
const krb5_data *inbuf, |
| 707 |
krb5_const_principal server, |
| 708 |
krb5_keyblock *keyblock, |
| 709 |
krb5_flags *ap_req_options, |
| 710 |
krb5_ticket **ticket) |
| 711 |
{ |
| 712 |
krb5_error_code ret; |
| 713 |
krb5_rd_req_in_ctx in; |
| 714 |
krb5_rd_req_out_ctx out; |
| 715 |
|
| 716 |
ret = krb5_rd_req_in_ctx_alloc(context, &in); |
| 717 |
if (ret) |
| 718 |
return ret; |
| 719 |
|
| 720 |
ret = krb5_rd_req_in_set_keyblock(context, in, keyblock); |
| 721 |
if (ret) { |
| 722 |
krb5_rd_req_in_ctx_free(context, in); |
| 723 |
return ret; |
| 724 |
} |
| 725 |
|
| 726 |
ret = krb5_rd_req_ctx(context, auth_context, inbuf, server, in, &out); |
| 727 |
krb5_rd_req_in_ctx_free(context, in); |
| 728 |
if (ret) |
| 729 |
return ret; |
| 730 |
|
| 731 |
if (ap_req_options) |
| 732 |
*ap_req_options = out->ap_req_options; |
| 733 |
if (ticket) { |
| 734 |
ret = krb5_copy_ticket(context, out->ticket, ticket); |
| 735 |
if (ret) |
| 736 |
goto out; |
| 737 |
} |
| 738 |
|
| 739 |
out: |
| 740 |
krb5_rd_req_out_ctx_free(context, out); |
| 741 |
return ret; |
| 742 |
} |
| 743 |
|
| 744 |
/* |
| 745 |
* |
| 746 |
*/ |
| 747 |
|
| 748 |
static krb5_error_code |
| 749 |
get_key_from_keytab(krb5_context context, |
| 750 |
krb5_ap_req *ap_req, |
| 751 |
krb5_const_principal server, |
| 752 |
krb5_keytab keytab, |
| 753 |
krb5_keyblock **out_key) |
| 754 |
{ |
| 755 |
krb5_keytab_entry entry; |
| 756 |
krb5_error_code ret; |
| 757 |
int kvno; |
| 758 |
krb5_keytab real_keytab; |
| 759 |
|
| 760 |
if(keytab == NULL) |
| 761 |
krb5_kt_default(context, &real_keytab); |
| 762 |
else |
| 763 |
real_keytab = keytab; |
| 764 |
|
| 765 |
if (ap_req->ticket.enc_part.kvno) |
| 766 |
kvno = *ap_req->ticket.enc_part.kvno; |
| 767 |
else |
| 768 |
kvno = 0; |
| 769 |
|
| 770 |
ret = krb5_kt_get_entry (context, |
| 771 |
real_keytab, |
| 772 |
server, |
| 773 |
kvno, |
| 774 |
ap_req->ticket.enc_part.etype, |
| 775 |
&entry); |
| 776 |
if(ret == 0) { |
| 777 |
ret = krb5_copy_keyblock(context, &entry.keyblock, out_key); |
| 778 |
krb5_kt_free_entry(context, &entry); |
| 779 |
} |
| 780 |
if(keytab == NULL) |
| 781 |
krb5_kt_close(context, real_keytab); |
| 782 |
|
| 783 |
return ret; |
| 784 |
} |
| 785 |
|
| 786 |
/** |
| 787 |
* The core server function that verify application authentication |
| 788 |
* requests from clients. |
| 789 |
* |
| 790 |
* @param context Keberos 5 context. |
| 791 |
* @param auth_context the authentication context, can be NULL, then |
| 792 |
* default values for the authentication context will used. |
| 793 |
* @param inbuf the (AP-REQ) authentication buffer |
| 794 |
* |
| 795 |
* @param server the server to authenticate to. If NULL the function |
| 796 |
* will try to find any available credential in the keytab |
| 797 |
* that will verify the reply. The function will prefer the |
| 798 |
* server specified in the AP-REQ, but if |
| 799 |
* there is no mach, it will try all keytab entries for a |
| 800 |
* match. This has serious performance issues for large keytabs. |
| 801 |
* |
| 802 |
* @param inctx control the behavior of the function, if NULL, the |
| 803 |
* default behavior is used. |
| 804 |
* @param outctx the return outctx, free with krb5_rd_req_out_ctx_free(). |
| 805 |
* @return Kerberos 5 error code, see krb5_get_error_message(). |
| 806 |
* |
| 807 |
* @ingroup krb5_auth |
| 808 |
*/ |
| 809 |
|
| 810 |
KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL |
| 811 |
krb5_rd_req_ctx(krb5_context context, |
| 812 |
krb5_auth_context *auth_context, |
| 813 |
const krb5_data *inbuf, |
| 814 |
krb5_const_principal server, |
| 815 |
krb5_rd_req_in_ctx inctx, |
| 816 |
krb5_rd_req_out_ctx *outctx) |
| 817 |
{ |
| 818 |
krb5_error_code ret; |
| 819 |
krb5_ap_req ap_req; |
| 820 |
krb5_rd_req_out_ctx o = NULL; |
| 821 |
krb5_keytab id = NULL, keytab = NULL; |
| 822 |
krb5_principal service = NULL; |
| 823 |
|
| 824 |
*outctx = NULL; |
| 825 |
|
| 826 |
o = calloc(1, sizeof(*o)); |
| 827 |
if (o == NULL) |
| 828 |
return krb5_enomem(context); |
| 829 |
|
| 830 |
if (*auth_context == NULL) { |
| 831 |
ret = krb5_auth_con_init(context, auth_context); |
| 832 |
if (ret) |
| 833 |
goto out; |
| 834 |
} |
| 835 |
|
| 836 |
ret = krb5_decode_ap_req(context, inbuf, &ap_req); |
| 837 |
if(ret) |
| 838 |
goto out; |
| 839 |
|
| 840 |
/* Save the principal that was in the request */ |
| 841 |
ret = _krb5_principalname2krb5_principal(context, |
| 842 |
&o->server, |
| 843 |
ap_req.ticket.sname, |
| 844 |
ap_req.ticket.realm); |
| 845 |
if (ret) |
| 846 |
goto out; |
| 847 |
|
| 848 |
if (ap_req.ap_options.use_session_key && |
| 849 |
(*auth_context)->keyblock == NULL) { |
| 850 |
ret = KRB5KRB_AP_ERR_NOKEY; |
| 851 |
krb5_set_error_message(context, ret, |
| 852 |
N_("krb5_rd_req: user to user auth " |
| 853 |
"without session key given", "")); |
| 854 |
goto out; |
| 855 |
} |
| 856 |
|
| 857 |
if (inctx && inctx->keytab) |
| 858 |
id = inctx->keytab; |
| 859 |
|
| 860 |
if((*auth_context)->keyblock){ |
| 861 |
ret = krb5_copy_keyblock(context, |
| 862 |
(*auth_context)->keyblock, |
| 863 |
&o->keyblock); |
| 864 |
if (ret) |
| 865 |
goto out; |
| 866 |
} else if(inctx && inctx->keyblock){ |
| 867 |
ret = krb5_copy_keyblock(context, |
| 868 |
inctx->keyblock, |
| 869 |
&o->keyblock); |
| 870 |
if (ret) |
| 871 |
goto out; |
| 872 |
} else { |
| 873 |
|
| 874 |
if(id == NULL) { |
| 875 |
krb5_kt_default(context, &keytab); |
| 876 |
id = keytab; |
| 877 |
} |
| 878 |
if (id == NULL) |
| 879 |
goto out; |
| 880 |
|
| 881 |
if (server == NULL) { |
| 882 |
ret = _krb5_principalname2krb5_principal(context, |
| 883 |
&service, |
| 884 |
ap_req.ticket.sname, |
| 885 |
ap_req.ticket.realm); |
| 886 |
if (ret) |
| 887 |
goto out; |
| 888 |
server = service; |
| 889 |
} |
| 890 |
|
| 891 |
ret = get_key_from_keytab(context, |
| 892 |
&ap_req, |
| 893 |
server, |
| 894 |
id, |
| 895 |
&o->keyblock); |
| 896 |
if (ret) { |
| 897 |
/* If caller specified a server, fail. */ |
| 898 |
if (service == NULL && (context->flags & KRB5_CTX_F_RD_REQ_IGNORE) == 0) |
| 899 |
goto out; |
| 900 |
/* Otherwise, fall back to iterating over the keytab. This |
| 901 |
* have serious performace issues for larger keytab. |
| 902 |
*/ |
| 903 |
o->keyblock = NULL; |
| 904 |
} |
| 905 |
} |
| 906 |
|
| 907 |
if (o->keyblock) { |
| 908 |
/* |
| 909 |
* We got an exact keymatch, use that. |
| 910 |
*/ |
| 911 |
|
| 912 |
ret = krb5_verify_ap_req2(context, |
| 913 |
auth_context, |
| 914 |
&ap_req, |
| 915 |
server, |
| 916 |
o->keyblock, |
| 917 |
0, |
| 918 |
&o->ap_req_options, |
| 919 |
&o->ticket, |
| 920 |
KRB5_KU_AP_REQ_AUTH); |
| 921 |
|
| 922 |
if (ret) |
| 923 |
goto out; |
| 924 |
|
| 925 |
} else { |
| 926 |
/* |
| 927 |
* Interate over keytab to find a key that can decrypt the request. |
| 928 |
*/ |
| 929 |
|
| 930 |
krb5_keytab_entry entry; |
| 931 |
krb5_kt_cursor cursor; |
| 932 |
int done = 0, kvno = 0; |
| 933 |
|
| 934 |
memset(&cursor, 0, sizeof(cursor)); |
| 935 |
|
| 936 |
if (ap_req.ticket.enc_part.kvno) |
| 937 |
kvno = *ap_req.ticket.enc_part.kvno; |
| 938 |
|
| 939 |
ret = krb5_kt_start_seq_get(context, id, &cursor); |
| 940 |
if (ret) |
| 941 |
goto out; |
| 942 |
|
| 943 |
done = 0; |
| 944 |
while (!done) { |
| 945 |
krb5_principal p; |
| 946 |
|
| 947 |
ret = krb5_kt_next_entry(context, id, &entry, &cursor); |
| 948 |
if (ret) { |
| 949 |
_krb5_kt_principal_not_found(context, ret, id, o->server, |
| 950 |
ap_req.ticket.enc_part.etype, |
| 951 |
kvno); |
| 952 |
break; |
| 953 |
} |
| 954 |
|
| 955 |
if (entry.keyblock.keytype != ap_req.ticket.enc_part.etype) { |
| 956 |
krb5_kt_free_entry (context, &entry); |
| 957 |
continue; |
| 958 |
} |
| 959 |
|
| 960 |
ret = krb5_verify_ap_req2(context, |
| 961 |
auth_context, |
| 962 |
&ap_req, |
| 963 |
server, |
| 964 |
&entry.keyblock, |
| 965 |
0, |
| 966 |
&o->ap_req_options, |
| 967 |
&o->ticket, |
| 968 |
KRB5_KU_AP_REQ_AUTH); |
| 969 |
if (ret) { |
| 970 |
krb5_kt_free_entry (context, &entry); |
| 971 |
continue; |
| 972 |
} |
| 973 |
|
| 974 |
/* |
| 975 |
* Found a match, save the keyblock for PAC processing, |
| 976 |
* and update the service principal in the ticket to match |
| 977 |
* whatever is in the keytab. |
| 978 |
*/ |
| 979 |
|
| 980 |
ret = krb5_copy_keyblock(context, |
| 981 |
&entry.keyblock, |
| 982 |
&o->keyblock); |
| 983 |
if (ret) { |
| 984 |
krb5_kt_free_entry (context, &entry); |
| 985 |
break; |
| 986 |
} |
| 987 |
|
| 988 |
ret = krb5_copy_principal(context, entry.principal, &p); |
| 989 |
if (ret) { |
| 990 |
krb5_kt_free_entry (context, &entry); |
| 991 |
break; |
| 992 |
} |
| 993 |
krb5_free_principal(context, o->ticket->server); |
| 994 |
o->ticket->server = p; |
| 995 |
|
| 996 |
krb5_kt_free_entry (context, &entry); |
| 997 |
|
| 998 |
done = 1; |
| 999 |
} |
| 1000 |
krb5_kt_end_seq_get (context, id, &cursor); |
| 1001 |
if (ret) |
| 1002 |
goto out; |
| 1003 |
} |
| 1004 |
|
| 1005 |
/* If there is a PAC, verify its server signature */ |
| 1006 |
if (inctx == NULL || inctx->check_pac) { |
| 1007 |
krb5_pac pac; |
| 1008 |
krb5_data data; |
| 1009 |
|
| 1010 |
ret = krb5_ticket_get_authorization_data_type(context, |
| 1011 |
o->ticket, |
| 1012 |
KRB5_AUTHDATA_WIN2K_PAC, |
| 1013 |
&data); |
| 1014 |
if (ret == 0) { |
| 1015 |
ret = krb5_pac_parse(context, data.data, data.length, &pac); |
| 1016 |
krb5_data_free(&data); |
| 1017 |
if (ret) |
| 1018 |
goto out; |
| 1019 |
|
| 1020 |
ret = krb5_pac_verify(context, |
| 1021 |
pac, |
| 1022 |
o->ticket->ticket.authtime, |
| 1023 |
o->ticket->client, |
| 1024 |
o->keyblock, |
| 1025 |
NULL); |
| 1026 |
krb5_pac_free(context, pac); |
| 1027 |
if (ret) |
| 1028 |
goto out; |
| 1029 |
} else |
| 1030 |
ret = 0; |
| 1031 |
} |
| 1032 |
out: |
| 1033 |
|
| 1034 |
if (ret || outctx == NULL) { |
| 1035 |
krb5_rd_req_out_ctx_free(context, o); |
| 1036 |
} else |
| 1037 |
*outctx = o; |
| 1038 |
|
| 1039 |
free_AP_REQ(&ap_req); |
| 1040 |
|
| 1041 |
if (service) |
| 1042 |
krb5_free_principal(context, service); |
| 1043 |
|
| 1044 |
if (keytab) |
| 1045 |
krb5_kt_close(context, keytab); |
| 1046 |
|
| 1047 |
return ret; |
| 1048 |
} |