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