3 * Copyright (c) 1997 - 2007 Kungliga Tekniska Högskolan
4 * (Royal Institute of Technology, Stockholm, Sweden).
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
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.
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.
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
35 #include "krb5_locl.h"
37 static krb5_error_code
38 decrypt_tkt_enc_part (krb5_context context
,
40 EncryptedData
*enc_part
,
41 EncTicketPart
*decr_part
)
48 ret
= krb5_crypto_init(context
, key
, 0, &crypto
);
51 ret
= krb5_decrypt_EncryptedData (context
,
56 krb5_crypto_destroy(context
, crypto
);
60 ret
= decode_EncTicketPart(plain
.data
, plain
.length
, decr_part
, &len
);
62 krb5_set_error_message(context
, ret
,
63 N_("Failed to decode encrypted "
65 krb5_data_free (&plain
);
69 static krb5_error_code
70 decrypt_authenticator (krb5_context context
,
72 EncryptedData
*enc_part
,
73 Authenticator
*authenticator
,
81 ret
= krb5_crypto_init(context
, key
, 0, &crypto
);
84 ret
= krb5_decrypt_EncryptedData (context
,
86 usage
/* KRB5_KU_AP_REQ_AUTH */,
89 /* for backwards compatibility, also try the old usage */
90 if (ret
&& usage
== KRB5_KU_TGS_REQ_AUTH
)
91 ret
= krb5_decrypt_EncryptedData (context
,
96 krb5_crypto_destroy(context
, crypto
);
100 ret
= decode_Authenticator(plain
.data
, plain
.length
,
101 authenticator
, &len
);
102 krb5_data_free (&plain
);
106 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
107 krb5_decode_ap_req(krb5_context context
,
108 const krb5_data
*inbuf
,
113 ret
= decode_AP_REQ(inbuf
->data
, inbuf
->length
, ap_req
, &len
);
116 if (ap_req
->pvno
!= 5){
118 krb5_clear_error_message (context
);
119 return KRB5KRB_AP_ERR_BADVERSION
;
121 if (ap_req
->msg_type
!= krb_ap_req
){
123 krb5_clear_error_message (context
);
124 return KRB5KRB_AP_ERR_MSG_TYPE
;
126 if (ap_req
->ticket
.tkt_vno
!= 5){
128 krb5_clear_error_message (context
);
129 return KRB5KRB_AP_ERR_BADVERSION
;
134 static krb5_error_code
135 check_transited(krb5_context context
, Ticket
*ticket
, EncTicketPart
*enc
)
138 unsigned int num_realms
, n
;
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.
146 if(enc
->transited
.tr_type
== 0 && enc
->transited
.contents
.length
== 0)
149 if(enc
->transited
.tr_type
!= domain_X500_Compress
)
150 return KRB5KDC_ERR_TRTYPE_NOSUPP
;
152 if(enc
->transited
.contents
.length
== 0)
155 ret
= krb5_domain_x500_decode(context
, enc
->transited
.contents
,
156 &realms
, &num_realms
,
161 ret
= krb5_check_transited(context
, enc
->crealm
,
163 realms
, num_realms
, NULL
);
164 for (n
= 0; n
< num_realms
; n
++)
170 static krb5_error_code
171 find_etypelist(krb5_context context
,
172 krb5_auth_context auth_context
,
178 ret
= _krb5_get_ad(context
, auth_context
->authenticator
->authorization_data
, NULL
, KRB5_AUTHDATA_GSS_API_ETYPE_NEGOTIATION
, &data
);
182 ret
= decode_EtypeList(data
.data
, data
.length
, etypes
, NULL
);
183 krb5_data_free(&data
);
185 krb5_clear_error_message(context
);
190 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
191 krb5_decrypt_ticket(krb5_context context
,
199 ret
= decrypt_tkt_enc_part (context
, key
, &ticket
->enc_part
, &t
);
205 time_t start
= t
.authtime
;
207 krb5_timeofday (context
, &now
);
209 start
= *t
.starttime
;
210 if(start
- now
> context
->max_skew
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
;
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
;
223 if(!t
.flags
.transited_policy_checked
) {
224 ret
= check_transited(context
, ticket
, &t
);
226 free_EncTicketPart(&t
);
235 free_EncTicketPart(&t
);
239 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
240 krb5_verify_authenticator_checksum(krb5_context context
,
241 krb5_auth_context ac
,
246 krb5_keyblock
*key
= NULL
;
247 krb5_authenticator authenticator
;
250 ret
= krb5_auth_con_getauthenticator(context
, ac
, &authenticator
);
253 if (authenticator
->cksum
== NULL
) {
257 ret
= krb5_auth_con_getkey(context
, ac
, &key
);
260 ret
= krb5_crypto_init(context
, key
, 0, &crypto
);
264 _krb5_crypto_set_flags(context
, crypto
, KRB5_CRYPTO_FLAG_ALLOW_UNKEYED_CHECKSUM
);
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
);
270 krb5_free_authenticator(context
, &authenticator
);
271 krb5_free_keyblock(context
, key
);
276 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
277 krb5_verify_ap_req(krb5_context context
,
278 krb5_auth_context
*auth_context
,
280 krb5_const_principal server
,
281 krb5_keyblock
*keyblock
,
283 krb5_flags
*ap_req_options
,
284 krb5_ticket
**ticket
)
286 return krb5_verify_ap_req2 (context
,
294 KRB5_KU_AP_REQ_AUTH
);
297 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
298 krb5_verify_ap_req2(krb5_context context
,
299 krb5_auth_context
*auth_context
,
301 krb5_const_principal server
,
302 krb5_keyblock
*keyblock
,
304 krb5_flags
*ap_req_options
,
305 krb5_ticket
**ticket
,
306 krb5_key_usage usage
)
309 krb5_auth_context ac
;
314 memset(&etypes
, 0, sizeof(etypes
));
319 if (auth_context
&& *auth_context
) {
322 ret
= krb5_auth_con_init (context
, &ac
);
327 t
= calloc(1, sizeof(*t
));
329 ret
= krb5_enomem(context
);
333 if (ap_req
->ap_options
.use_session_key
&& ac
->keyblock
){
334 ret
= krb5_decrypt_ticket(context
, &ap_req
->ticket
,
338 krb5_free_keyblock(context
, ac
->keyblock
);
341 ret
= krb5_decrypt_ticket(context
, &ap_req
->ticket
,
349 ret
= _krb5_principalname2krb5_principal(context
,
351 ap_req
->ticket
.sname
,
352 ap_req
->ticket
.realm
);
355 ret
= decrypt_authenticator (context
,
357 &ap_req
->authenticator
,
364 krb5_principal p1
, p2
;
367 _krb5_principalname2krb5_principal(context
,
369 ac
->authenticator
->cname
,
370 ac
->authenticator
->crealm
);
371 _krb5_principalname2krb5_principal(context
,
375 res
= krb5_principal_compare (context
, p1
, p2
);
376 krb5_free_principal (context
, p1
);
377 krb5_free_principal (context
, p2
);
379 ret
= KRB5KRB_AP_ERR_BADMATCH
;
380 krb5_clear_error_message (context
);
386 * The ticket authenticates the client, and conveys naming attributes that
387 * we want to expose in GSS using RFC6680 APIs.
389 * So we same the ticket enc-part in the client's krb5_principal object
390 * (note though that the session key will be absent in that copy of the
393 ret
= _krb5_ticket2krb5_principal(context
, &t
->client
, &t
->ticket
,
394 ac
->authenticator
->authorization_data
);
397 t
->client
->nameattrs
->peer_realm
=
398 calloc(1, sizeof(t
->client
->nameattrs
->peer_realm
[0]));
399 if (t
->client
->nameattrs
->peer_realm
== NULL
) {
400 ret
= krb5_enomem(context
);
403 ret
= copy_Realm(&ap_req
->ticket
.realm
, t
->client
->nameattrs
->peer_realm
);
406 /* check addresses */
409 && ac
->remote_address
410 && !krb5_address_search (context
,
414 * Hack alert. If KRB5_VERIFY_AP_REQ_IGNORE_ADDRS and the client's
415 * address didn't check out then we'll return KRB5KRB_AP_ERR_BADADDR
416 * even on success, and we'll let the caller figure it out because
417 * `*ticket != NULL' or `*auth_context != NULL'.
419 if ((flags
& KRB5_VERIFY_AP_REQ_IGNORE_ADDRS
)) {
422 ret
= KRB5KRB_AP_ERR_BADADDR
;
423 krb5_clear_error_message(context
);
428 /* check timestamp in authenticator */
432 krb5_timeofday (context
, &now
);
434 if (krb5_time_abs(ac
->authenticator
->ctime
, now
) > context
->max_skew
) {
435 ret
= KRB5KRB_AP_ERR_SKEW
;
436 krb5_clear_error_message (context
);
441 if (ac
->authenticator
->seq_number
)
442 krb5_auth_con_setremoteseqnumber(context
, ac
,
443 *ac
->authenticator
->seq_number
);
445 /* XXX - Xor sequence numbers */
447 if (ac
->authenticator
->subkey
) {
448 ret
= krb5_auth_con_setremotesubkey(context
, ac
,
449 ac
->authenticator
->subkey
);
454 ret
= find_etypelist(context
, ac
, &etypes
);
458 ac
->keytype
= ETYPE_NULL
;
463 for (i
= 0; i
< etypes
.len
; i
++) {
464 if (krb5_enctype_valid(context
, etypes
.val
[i
]) == 0) {
465 ac
->keytype
= etypes
.val
[i
];
472 ret
= krb5_copy_keyblock(context
, &t
->ticket
.key
, &ac
->keyblock
);
475 if (ap_req_options
) {
477 if (ac
->keytype
!= ETYPE_NULL
)
478 *ap_req_options
|= AP_OPTS_USE_SUBKEY
;
479 if (ap_req
->ap_options
.use_session_key
)
480 *ap_req_options
|= AP_OPTS_USE_SESSION_KEY
;
481 if (ap_req
->ap_options
.mutual_required
)
482 *ap_req_options
|= AP_OPTS_MUTUAL_REQUIRED
;
488 krb5_free_ticket (context
, t
);
490 if (*auth_context
== NULL
)
493 krb5_auth_con_free (context
, ac
);
494 free_EtypeList(&etypes
);
497 krb5_clear_error_message(context
);
498 return KRB5KRB_AP_ERR_BADADDR
;
502 free_EtypeList(&etypes
);
504 krb5_free_ticket (context
, t
);
505 if (auth_context
== NULL
|| *auth_context
== NULL
)
506 krb5_auth_con_free (context
, ac
);
514 struct krb5_rd_req_in_ctx_data
{
516 krb5_keyblock
*keyblock
;
517 krb5_boolean check_pac
;
520 struct krb5_rd_req_out_ctx_data
{
521 krb5_keyblock
*keyblock
;
522 krb5_flags ap_req_options
;
524 krb5_principal server
;
528 * Allocate a krb5_rd_req_in_ctx as an input parameter to
529 * krb5_rd_req_ctx(). The caller should free the context with
530 * krb5_rd_req_in_ctx_free() when done with the context.
532 * @param context Keberos 5 context.
533 * @param ctx in ctx to krb5_rd_req_ctx().
535 * @return Kerberos 5 error code, see krb5_get_error_message().
540 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
541 krb5_rd_req_in_ctx_alloc(krb5_context context
, krb5_rd_req_in_ctx
*ctx
)
543 *ctx
= calloc(1, sizeof(**ctx
));
545 return krb5_enomem(context
);
546 (*ctx
)->check_pac
= (context
->flags
& KRB5_CTX_F_CHECK_PAC
) ? 1 : 0;
551 * Set the keytab that krb5_rd_req_ctx() will use.
553 * @param context Keberos 5 context.
554 * @param in in ctx to krb5_rd_req_ctx().
555 * @param keytab keytab that krb5_rd_req_ctx() will use, only copy the
556 * pointer, so the caller must free they keytab after
557 * krb5_rd_req_in_ctx_free() is called.
559 * @return Kerberos 5 error code, see krb5_get_error_message().
564 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
565 krb5_rd_req_in_set_keytab(krb5_context context
,
566 krb5_rd_req_in_ctx in
,
574 * Set if krb5_rq_red() is going to check the Windows PAC or not
576 * @param context Keberos 5 context.
577 * @param in krb5_rd_req_in_ctx to check the option on.
578 * @param flag flag to select if to check the pac (TRUE) or not (FALSE).
580 * @return Kerberos 5 error code, see krb5_get_error_message().
585 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
586 krb5_rd_req_in_set_pac_check(krb5_context context
,
587 krb5_rd_req_in_ctx in
,
590 in
->check_pac
= flag
;
595 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
596 krb5_rd_req_in_set_keyblock(krb5_context context
,
597 krb5_rd_req_in_ctx in
,
598 krb5_keyblock
*keyblock
)
600 in
->keyblock
= keyblock
; /* XXX should make copy */
604 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
605 krb5_rd_req_out_get_ap_req_options(krb5_context context
,
606 krb5_rd_req_out_ctx out
,
607 krb5_flags
*ap_req_options
)
609 *ap_req_options
= out
->ap_req_options
;
613 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
614 krb5_rd_req_out_get_ticket(krb5_context context
,
615 krb5_rd_req_out_ctx out
,
616 krb5_ticket
**ticket
)
618 return krb5_copy_ticket(context
, out
->ticket
, ticket
);
621 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
622 krb5_rd_req_out_get_keyblock(krb5_context context
,
623 krb5_rd_req_out_ctx out
,
624 krb5_keyblock
**keyblock
)
626 return krb5_copy_keyblock(context
, out
->keyblock
, keyblock
);
630 * Get the principal that was used in the request from the
631 * client. Might not match whats in the ticket if krb5_rd_req_ctx()
632 * searched in the keytab for a matching key.
634 * @param context a Kerberos 5 context.
635 * @param out a krb5_rd_req_out_ctx from krb5_rd_req_ctx().
636 * @param principal return principal, free with krb5_free_principal().
641 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
642 krb5_rd_req_out_get_server(krb5_context context
,
643 krb5_rd_req_out_ctx out
,
644 krb5_principal
*principal
)
646 return krb5_copy_principal(context
, out
->server
, principal
);
649 KRB5_LIB_FUNCTION
void KRB5_LIB_CALL
650 krb5_rd_req_in_ctx_free(krb5_context context
, krb5_rd_req_in_ctx ctx
)
656 * Free the krb5_rd_req_out_ctx.
658 * @param context Keberos 5 context.
659 * @param ctx krb5_rd_req_out_ctx context to free.
664 KRB5_LIB_FUNCTION
void KRB5_LIB_CALL
665 krb5_rd_req_out_ctx_free(krb5_context context
, krb5_rd_req_out_ctx ctx
)
668 krb5_free_ticket(context
, ctx
->ticket
);
670 krb5_free_keyblock(context
, ctx
->keyblock
);
672 krb5_free_principal(context
, ctx
->server
);
677 * Process an AP_REQ message.
679 * @param context Kerberos 5 context.
680 * @param auth_context authentication context of the peer.
681 * @param inbuf the AP_REQ message, obtained for example with krb5_read_message().
682 * @param server server principal.
683 * @param keytab server keytab.
684 * @param ap_req_options set to the AP_REQ options. See the AP_OPTS_* defines.
685 * @param ticket on success, set to the authenticated client credentials.
686 * Must be deallocated with krb5_free_ticket(). If not
687 * interested, pass a NULL value.
689 * @return 0 to indicate success. Otherwise a Kerberos error code is
690 * returned, see krb5_get_error_message().
692 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
693 krb5_rd_req(krb5_context context
,
694 krb5_auth_context
*auth_context
,
695 const krb5_data
*inbuf
,
696 krb5_const_principal server
,
698 krb5_flags
*ap_req_options
,
699 krb5_ticket
**ticket
)
702 krb5_rd_req_in_ctx in
;
703 krb5_rd_req_out_ctx out
;
705 ret
= krb5_rd_req_in_ctx_alloc(context
, &in
);
709 ret
= krb5_rd_req_in_set_keytab(context
, in
, keytab
);
711 krb5_rd_req_in_ctx_free(context
, in
);
715 ret
= krb5_rd_req_ctx(context
, auth_context
, inbuf
, server
, in
, &out
);
716 krb5_rd_req_in_ctx_free(context
, in
);
721 *ap_req_options
= out
->ap_req_options
;
723 ret
= krb5_copy_ticket(context
, out
->ticket
, ticket
);
729 krb5_rd_req_out_ctx_free(context
, out
);
737 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
738 krb5_rd_req_with_keyblock(krb5_context context
,
739 krb5_auth_context
*auth_context
,
740 const krb5_data
*inbuf
,
741 krb5_const_principal server
,
742 krb5_keyblock
*keyblock
,
743 krb5_flags
*ap_req_options
,
744 krb5_ticket
**ticket
)
747 krb5_rd_req_in_ctx in
;
748 krb5_rd_req_out_ctx out
;
750 ret
= krb5_rd_req_in_ctx_alloc(context
, &in
);
754 ret
= krb5_rd_req_in_set_keyblock(context
, in
, keyblock
);
756 krb5_rd_req_in_ctx_free(context
, in
);
760 ret
= krb5_rd_req_ctx(context
, auth_context
, inbuf
, server
, in
, &out
);
761 krb5_rd_req_in_ctx_free(context
, in
);
766 *ap_req_options
= out
->ap_req_options
;
768 ret
= krb5_copy_ticket(context
, out
->ticket
, ticket
);
774 krb5_rd_req_out_ctx_free(context
, out
);
782 static krb5_error_code
783 get_key_from_keytab(krb5_context context
,
785 krb5_const_principal server
,
787 krb5_keyblock
**out_key
)
789 krb5_keytab_entry entry
;
792 krb5_keytab real_keytab
;
795 krb5_kt_default(context
, &real_keytab
);
797 real_keytab
= keytab
;
799 if (ap_req
->ticket
.enc_part
.kvno
)
800 kvno
= *ap_req
->ticket
.enc_part
.kvno
;
804 ret
= krb5_kt_get_entry (context
,
808 ap_req
->ticket
.enc_part
.etype
,
811 ret
= krb5_copy_keyblock(context
, &entry
.keyblock
, out_key
);
812 krb5_kt_free_entry(context
, &entry
);
815 krb5_kt_close(context
, real_keytab
);
821 * The core server function that verify application authentication
822 * requests from clients.
824 * @param context Keberos 5 context.
825 * @param auth_context the authentication context, can be NULL, then
826 * default values for the authentication context will used.
827 * @param inbuf the (AP-REQ) authentication buffer
829 * @param server the server to authenticate to. If NULL the function
830 * will try to find any available credential in the keytab
831 * that will verify the reply. The function will prefer the
832 * server specified in the AP-REQ, but if
833 * there is no mach, it will try all keytab entries for a
834 * match. This has serious performance issues for large keytabs.
836 * @param inctx control the behavior of the function, if NULL, the
837 * default behavior is used.
838 * @param outctx the return outctx, free with krb5_rd_req_out_ctx_free().
839 * @return Kerberos 5 error code, see krb5_get_error_message().
844 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
845 krb5_rd_req_ctx(krb5_context context
,
846 krb5_auth_context
*auth_context
,
847 const krb5_data
*inbuf
,
848 krb5_const_principal server
,
849 krb5_rd_req_in_ctx inctx
,
850 krb5_rd_req_out_ctx
*outctx
)
854 krb5_rd_req_out_ctx o
= NULL
;
855 krb5_keytab id
= NULL
, keytab
= NULL
;
856 krb5_principal service
= NULL
;
861 o
= calloc(1, sizeof(*o
));
863 return krb5_enomem(context
);
865 if (*auth_context
== NULL
) {
866 ret
= krb5_auth_con_init(context
, auth_context
);
871 ret
= krb5_decode_ap_req(context
, inbuf
, &ap_req
);
875 /* Save the principal that was in the request */
876 ret
= _krb5_principalname2krb5_principal(context
,
879 ap_req
.ticket
.realm
);
883 if (ap_req
.ap_options
.use_session_key
&&
884 (*auth_context
)->keyblock
== NULL
) {
885 ret
= KRB5KRB_AP_ERR_NOKEY
;
886 krb5_set_error_message(context
, ret
,
887 N_("krb5_rd_req: user to user auth "
888 "without session key given", ""));
892 if (inctx
&& inctx
->keytab
)
895 if((*auth_context
)->keyblock
){
896 ret
= krb5_copy_keyblock(context
,
897 (*auth_context
)->keyblock
,
901 } else if(inctx
&& inctx
->keyblock
){
902 ret
= krb5_copy_keyblock(context
,
910 krb5_kt_default(context
, &keytab
);
916 if (server
== NULL
) {
917 ret
= _krb5_principalname2krb5_principal(context
,
920 ap_req
.ticket
.realm
);
926 ret
= get_key_from_keytab(context
,
932 /* If caller specified a server, fail. */
933 if (service
== NULL
&& (context
->flags
& KRB5_CTX_F_RD_REQ_IGNORE
) == 0)
935 /* Otherwise, fall back to iterating over the keytab. This
936 * have serious performace issues for larger keytab.
944 * We got an exact keymatch, use that.
947 ret
= krb5_verify_ap_req2(context
,
955 KRB5_KU_AP_REQ_AUTH
);
962 * Interate over keytab to find a key that can decrypt the request.
965 krb5_keytab_entry entry
;
966 krb5_kt_cursor cursor
;
967 int done
= 0, kvno
= 0;
969 memset(&cursor
, 0, sizeof(cursor
));
971 if (ap_req
.ticket
.enc_part
.kvno
)
972 kvno
= *ap_req
.ticket
.enc_part
.kvno
;
974 ret
= krb5_kt_start_seq_get(context
, id
, &cursor
);
982 ret
= krb5_kt_next_entry(context
, id
, &entry
, &cursor
);
984 _krb5_kt_principal_not_found(context
, ret
, id
, o
->server
,
985 ap_req
.ticket
.enc_part
.etype
,
990 if (entry
.keyblock
.keytype
!= ap_req
.ticket
.enc_part
.etype
) {
991 krb5_kt_free_entry (context
, &entry
);
995 ret
= krb5_verify_ap_req2(context
,
1003 KRB5_KU_AP_REQ_AUTH
);
1005 krb5_kt_free_entry (context
, &entry
);
1010 * Found a match, save the keyblock for PAC processing,
1011 * and update the service principal in the ticket to match
1012 * whatever is in the keytab.
1015 ret
= krb5_copy_keyblock(context
,
1019 krb5_kt_free_entry (context
, &entry
);
1023 ret
= krb5_copy_principal(context
, entry
.principal
, &p
);
1025 krb5_kt_free_entry (context
, &entry
);
1028 krb5_free_principal(context
, o
->ticket
->server
);
1029 o
->ticket
->server
= p
;
1031 krb5_kt_free_entry (context
, &entry
);
1035 krb5_kt_end_seq_get (context
, id
, &cursor
);
1040 if (krb5_ticket_get_authorization_data_type(context
, o
->ticket
,
1041 KRB5_AUTHDATA_KDC_ISSUED
,
1043 o
->ticket
->client
->nameattrs
->kdc_issued_verified
= 1;
1045 /* If there is a PAC, verify its server signature */
1046 if (inctx
== NULL
|| inctx
->check_pac
) {
1050 ret
= krb5_ticket_get_authorization_data_type(context
,
1052 KRB5_AUTHDATA_WIN2K_PAC
,
1055 ret
= krb5_pac_parse(context
, data
.data
, data
.length
, &pac
);
1056 krb5_data_free(&data
);
1060 ret
= krb5_pac_verify(context
,
1062 o
->ticket
->ticket
.authtime
,
1067 o
->ticket
->client
->nameattrs
->pac_verified
= 1;
1068 if (ret
== 0 && (context
->flags
& KRB5_CTX_F_REPORT_CANONICAL_CLIENT_NAME
)) {
1069 krb5_error_code ret2
;
1070 krb5_principal canon_name
;
1072 ret2
= _krb5_pac_get_canon_principal(context
, pac
, &canon_name
);
1074 free_Realm(&o
->ticket
->client
->realm
);
1075 free_PrincipalName(&o
->ticket
->client
->name
);
1076 ret
= copy_Realm(&canon_name
->realm
, &o
->ticket
->client
->realm
);
1078 ret
= copy_PrincipalName(&canon_name
->name
, &o
->ticket
->client
->name
);
1079 krb5_free_principal(context
, canon_name
);
1080 } else if (ret2
!= ENOENT
)
1084 krb5_pac_free(context
, pac
);
1087 o
->ticket
->client
->nameattrs
->pac
= pac
;
1093 if (ret
|| outctx
== NULL
)
1094 krb5_rd_req_out_ctx_free(context
, o
);
1098 free_AP_REQ(&ap_req
);
1101 krb5_free_principal(context
, service
);
1104 krb5_kt_close(context
, keytab
);