2 * Copyright (c) 1997 - 2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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
34 #include <krb5_locl.h>
36 __RCSID("$Heimdal: rd_req.c 22235 2007-12-08 21:52:07Z lha $"
39 static krb5_error_code
40 decrypt_tkt_enc_part (krb5_context context
,
42 EncryptedData
*enc_part
,
43 EncTicketPart
*decr_part
)
50 ret
= krb5_crypto_init(context
, key
, 0, &crypto
);
53 ret
= krb5_decrypt_EncryptedData (context
,
58 krb5_crypto_destroy(context
, crypto
);
62 ret
= krb5_decode_EncTicketPart(context
, plain
.data
, plain
.length
,
64 krb5_data_free (&plain
);
68 static krb5_error_code
69 decrypt_authenticator (krb5_context context
,
71 EncryptedData
*enc_part
,
72 Authenticator
*authenticator
,
80 ret
= krb5_crypto_init(context
, key
, 0, &crypto
);
83 ret
= krb5_decrypt_EncryptedData (context
,
85 usage
/* KRB5_KU_AP_REQ_AUTH */,
88 /* for backwards compatibility, also try the old usage */
89 if (ret
&& usage
== KRB5_KU_TGS_REQ_AUTH
)
90 ret
= krb5_decrypt_EncryptedData (context
,
95 krb5_crypto_destroy(context
, crypto
);
99 ret
= krb5_decode_Authenticator(context
, plain
.data
, plain
.length
,
100 authenticator
, &len
);
101 krb5_data_free (&plain
);
105 krb5_error_code KRB5_LIB_FUNCTION
106 krb5_decode_ap_req(krb5_context context
,
107 const krb5_data
*inbuf
,
112 ret
= decode_AP_REQ(inbuf
->data
, inbuf
->length
, ap_req
, &len
);
115 if (ap_req
->pvno
!= 5){
117 krb5_clear_error_string (context
);
118 return KRB5KRB_AP_ERR_BADVERSION
;
120 if (ap_req
->msg_type
!= krb_ap_req
){
122 krb5_clear_error_string (context
);
123 return KRB5KRB_AP_ERR_MSG_TYPE
;
125 if (ap_req
->ticket
.tkt_vno
!= 5){
127 krb5_clear_error_string (context
);
128 return KRB5KRB_AP_ERR_BADVERSION
;
133 static krb5_error_code
134 check_transited(krb5_context context
, Ticket
*ticket
, EncTicketPart
*enc
)
141 * Windows 2000 and 2003 uses this inside their TGT so it's normaly
142 * not seen by others, however, samba4 joined with a Windows AD as
143 * a Domain Controller gets exposed to this.
145 if(enc
->transited
.tr_type
== 0 && enc
->transited
.contents
.length
== 0)
148 if(enc
->transited
.tr_type
!= DOMAIN_X500_COMPRESS
)
149 return KRB5KDC_ERR_TRTYPE_NOSUPP
;
151 if(enc
->transited
.contents
.length
== 0)
154 ret
= krb5_domain_x500_decode(context
, enc
->transited
.contents
,
155 &realms
, &num_realms
,
160 ret
= krb5_check_transited(context
, enc
->crealm
,
162 realms
, num_realms
, NULL
);
167 static krb5_error_code
168 find_etypelist(krb5_context context
,
169 krb5_auth_context auth_context
,
174 krb5_authdata adIfRelevant
;
177 adIfRelevant
.len
= 0;
182 ad
= auth_context
->authenticator
->authorization_data
;
186 for (i
= 0; i
< ad
->len
; i
++) {
187 if (ad
->val
[i
].ad_type
== KRB5_AUTHDATA_IF_RELEVANT
) {
188 ret
= decode_AD_IF_RELEVANT(ad
->val
[i
].ad_data
.data
,
189 ad
->val
[i
].ad_data
.length
,
195 if (adIfRelevant
.len
== 1 &&
196 adIfRelevant
.val
[0].ad_type
==
197 KRB5_AUTHDATA_GSS_API_ETYPE_NEGOTIATION
) {
200 free_AD_IF_RELEVANT(&adIfRelevant
);
201 adIfRelevant
.len
= 0;
205 if (adIfRelevant
.len
== 0)
208 ret
= decode_EtypeList(adIfRelevant
.val
[0].ad_data
.data
,
209 adIfRelevant
.val
[0].ad_data
.length
,
213 krb5_clear_error_string(context
);
215 free_AD_IF_RELEVANT(&adIfRelevant
);
220 krb5_error_code KRB5_LIB_FUNCTION
221 krb5_decrypt_ticket(krb5_context context
,
229 ret
= decrypt_tkt_enc_part (context
, key
, &ticket
->enc_part
, &t
);
235 time_t start
= t
.authtime
;
237 krb5_timeofday (context
, &now
);
239 start
= *t
.starttime
;
240 if(start
- now
> context
->max_skew
242 && !(flags
& KRB5_VERIFY_AP_REQ_IGNORE_INVALID
))) {
243 free_EncTicketPart(&t
);
244 krb5_clear_error_string (context
);
245 return KRB5KRB_AP_ERR_TKT_NYV
;
247 if(now
- t
.endtime
> context
->max_skew
) {
248 free_EncTicketPart(&t
);
249 krb5_clear_error_string (context
);
250 return KRB5KRB_AP_ERR_TKT_EXPIRED
;
253 if(!t
.flags
.transited_policy_checked
) {
254 ret
= check_transited(context
, ticket
, &t
);
256 free_EncTicketPart(&t
);
265 free_EncTicketPart(&t
);
269 krb5_error_code KRB5_LIB_FUNCTION
270 krb5_verify_authenticator_checksum(krb5_context context
,
271 krb5_auth_context ac
,
277 krb5_authenticator authenticator
;
280 ret
= krb5_auth_con_getauthenticator (context
,
285 if(authenticator
->cksum
== NULL
) {
286 krb5_free_authenticator(context
, &authenticator
);
289 ret
= krb5_auth_con_getkey(context
, ac
, &key
);
291 krb5_free_authenticator(context
, &authenticator
);
294 ret
= krb5_crypto_init(context
, key
, 0, &crypto
);
297 ret
= krb5_verify_checksum (context
,
299 KRB5_KU_AP_REQ_AUTH_CKSUM
,
302 authenticator
->cksum
);
303 krb5_crypto_destroy(context
, crypto
);
305 krb5_free_authenticator(context
, &authenticator
);
306 krb5_free_keyblock(context
, key
);
311 krb5_error_code KRB5_LIB_FUNCTION
312 krb5_verify_ap_req(krb5_context context
,
313 krb5_auth_context
*auth_context
,
315 krb5_const_principal server
,
316 krb5_keyblock
*keyblock
,
318 krb5_flags
*ap_req_options
,
319 krb5_ticket
**ticket
)
321 return krb5_verify_ap_req2 (context
,
329 KRB5_KU_AP_REQ_AUTH
);
332 krb5_error_code KRB5_LIB_FUNCTION
333 krb5_verify_ap_req2(krb5_context context
,
334 krb5_auth_context
*auth_context
,
336 krb5_const_principal server
,
337 krb5_keyblock
*keyblock
,
339 krb5_flags
*ap_req_options
,
340 krb5_ticket
**ticket
,
341 krb5_key_usage usage
)
344 krb5_auth_context ac
;
351 if (auth_context
&& *auth_context
) {
354 ret
= krb5_auth_con_init (context
, &ac
);
359 t
= calloc(1, sizeof(*t
));
362 krb5_clear_error_string (context
);
366 if (ap_req
->ap_options
.use_session_key
&& ac
->keyblock
){
367 ret
= krb5_decrypt_ticket(context
, &ap_req
->ticket
,
371 krb5_free_keyblock(context
, ac
->keyblock
);
374 ret
= krb5_decrypt_ticket(context
, &ap_req
->ticket
,
382 ret
= _krb5_principalname2krb5_principal(context
,
384 ap_req
->ticket
.sname
,
385 ap_req
->ticket
.realm
);
387 ret
= _krb5_principalname2krb5_principal(context
,
395 ret
= krb5_copy_keyblock(context
, &t
->ticket
.key
, &ac
->keyblock
);
398 ret
= decrypt_authenticator (context
,
400 &ap_req
->authenticator
,
407 krb5_principal p1
, p2
;
410 _krb5_principalname2krb5_principal(context
,
412 ac
->authenticator
->cname
,
413 ac
->authenticator
->crealm
);
414 _krb5_principalname2krb5_principal(context
,
418 res
= krb5_principal_compare (context
, p1
, p2
);
419 krb5_free_principal (context
, p1
);
420 krb5_free_principal (context
, p2
);
422 ret
= KRB5KRB_AP_ERR_BADMATCH
;
423 krb5_clear_error_string (context
);
428 /* check addresses */
431 && ac
->remote_address
432 && !krb5_address_search (context
,
435 ret
= KRB5KRB_AP_ERR_BADADDR
;
436 krb5_clear_error_string (context
);
440 /* check timestamp in authenticator */
444 krb5_timeofday (context
, &now
);
446 if (abs(ac
->authenticator
->ctime
- now
) > context
->max_skew
) {
447 ret
= KRB5KRB_AP_ERR_SKEW
;
448 krb5_clear_error_string (context
);
453 if (ac
->authenticator
->seq_number
)
454 krb5_auth_con_setremoteseqnumber(context
, ac
,
455 *ac
->authenticator
->seq_number
);
457 /* XXX - Xor sequence numbers */
459 if (ac
->authenticator
->subkey
) {
460 ret
= krb5_auth_con_setremotesubkey(context
, ac
,
461 ac
->authenticator
->subkey
);
466 ret
= find_etypelist(context
, ac
, &etypes
);
470 ac
->keytype
= ETYPE_NULL
;
475 for (i
= 0; i
< etypes
.len
; i
++) {
476 if (krb5_enctype_valid(context
, etypes
.val
[i
]) == 0) {
477 ac
->keytype
= etypes
.val
[i
];
483 if (ap_req_options
) {
485 if (ac
->keytype
!= ETYPE_NULL
)
486 *ap_req_options
|= AP_OPTS_USE_SUBKEY
;
487 if (ap_req
->ap_options
.use_session_key
)
488 *ap_req_options
|= AP_OPTS_USE_SESSION_KEY
;
489 if (ap_req
->ap_options
.mutual_required
)
490 *ap_req_options
|= AP_OPTS_MUTUAL_REQUIRED
;
496 krb5_free_ticket (context
, t
);
498 if (*auth_context
== NULL
)
501 krb5_auth_con_free (context
, ac
);
502 free_EtypeList(&etypes
);
506 krb5_free_ticket (context
, t
);
507 if (auth_context
== NULL
|| *auth_context
== NULL
)
508 krb5_auth_con_free (context
, ac
);
516 struct krb5_rd_req_in_ctx_data
{
518 krb5_keyblock
*keyblock
;
519 krb5_boolean check_pac
;
522 struct krb5_rd_req_out_ctx_data
{
523 krb5_keyblock
*keyblock
;
524 krb5_flags ap_req_options
;
532 krb5_error_code KRB5_LIB_FUNCTION
533 krb5_rd_req_in_ctx_alloc(krb5_context context
, krb5_rd_req_in_ctx
*ctx
)
535 *ctx
= calloc(1, sizeof(**ctx
));
537 krb5_set_error_string(context
, "out of memory");
540 (*ctx
)->check_pac
= (context
->flags
& KRB5_CTX_F_CHECK_PAC
) ? 1 : 0;
544 krb5_error_code KRB5_LIB_FUNCTION
545 krb5_rd_req_in_set_keytab(krb5_context context
,
546 krb5_rd_req_in_ctx in
,
549 in
->keytab
= keytab
; /* XXX should make copy */
554 * Set if krb5_rq_red() is going to check the Windows PAC or not
556 * @param context Keberos 5 context.
557 * @param in krb5_rd_req_in_ctx to check the option on.
558 * @param flag flag to select if to check the pac (TRUE) or not (FALSE).
560 * @return Kerberos 5 error code, see krb5_get_error_message().
565 krb5_error_code KRB5_LIB_FUNCTION
566 krb5_rd_req_in_set_pac_check(krb5_context context
,
567 krb5_rd_req_in_ctx in
,
570 in
->check_pac
= flag
;
575 krb5_error_code KRB5_LIB_FUNCTION
576 krb5_rd_req_in_set_keyblock(krb5_context context
,
577 krb5_rd_req_in_ctx in
,
578 krb5_keyblock
*keyblock
)
580 in
->keyblock
= keyblock
; /* XXX should make copy */
584 krb5_error_code KRB5_LIB_FUNCTION
585 krb5_rd_req_out_get_ap_req_options(krb5_context context
,
586 krb5_rd_req_out_ctx out
,
587 krb5_flags
*ap_req_options
)
589 *ap_req_options
= out
->ap_req_options
;
593 krb5_error_code KRB5_LIB_FUNCTION
594 krb5_rd_req_out_get_ticket(krb5_context context
,
595 krb5_rd_req_out_ctx out
,
596 krb5_ticket
**ticket
)
598 return krb5_copy_ticket(context
, out
->ticket
, ticket
);
601 krb5_error_code KRB5_LIB_FUNCTION
602 krb5_rd_req_out_get_keyblock(krb5_context context
,
603 krb5_rd_req_out_ctx out
,
604 krb5_keyblock
**keyblock
)
606 return krb5_copy_keyblock(context
, out
->keyblock
, keyblock
);
609 void KRB5_LIB_FUNCTION
610 krb5_rd_req_in_ctx_free(krb5_context context
, krb5_rd_req_in_ctx ctx
)
615 krb5_error_code KRB5_LIB_FUNCTION
616 _krb5_rd_req_out_ctx_alloc(krb5_context context
, krb5_rd_req_out_ctx
*ctx
)
618 *ctx
= calloc(1, sizeof(**ctx
));
620 krb5_set_error_string(context
, "out of memory");
626 void KRB5_LIB_FUNCTION
627 krb5_rd_req_out_ctx_free(krb5_context context
, krb5_rd_req_out_ctx ctx
)
629 krb5_free_keyblock(context
, ctx
->keyblock
);
637 krb5_error_code KRB5_LIB_FUNCTION
638 krb5_rd_req(krb5_context context
,
639 krb5_auth_context
*auth_context
,
640 const krb5_data
*inbuf
,
641 krb5_const_principal server
,
643 krb5_flags
*ap_req_options
,
644 krb5_ticket
**ticket
)
647 krb5_rd_req_in_ctx in
;
648 krb5_rd_req_out_ctx out
;
650 ret
= krb5_rd_req_in_ctx_alloc(context
, &in
);
654 ret
= krb5_rd_req_in_set_keytab(context
, in
, keytab
);
656 krb5_rd_req_in_ctx_free(context
, in
);
660 ret
= krb5_rd_req_ctx(context
, auth_context
, inbuf
, server
, in
, &out
);
661 krb5_rd_req_in_ctx_free(context
, in
);
666 *ap_req_options
= out
->ap_req_options
;
668 ret
= krb5_copy_ticket(context
, out
->ticket
, ticket
);
674 krb5_rd_req_out_ctx_free(context
, out
);
682 krb5_error_code KRB5_LIB_FUNCTION
683 krb5_rd_req_with_keyblock(krb5_context context
,
684 krb5_auth_context
*auth_context
,
685 const krb5_data
*inbuf
,
686 krb5_const_principal server
,
687 krb5_keyblock
*keyblock
,
688 krb5_flags
*ap_req_options
,
689 krb5_ticket
**ticket
)
692 krb5_rd_req_in_ctx in
;
693 krb5_rd_req_out_ctx out
;
695 ret
= krb5_rd_req_in_ctx_alloc(context
, &in
);
699 ret
= krb5_rd_req_in_set_keyblock(context
, in
, keyblock
);
701 krb5_rd_req_in_ctx_free(context
, in
);
705 ret
= krb5_rd_req_ctx(context
, auth_context
, inbuf
, server
, in
, &out
);
706 krb5_rd_req_in_ctx_free(context
, in
);
711 *ap_req_options
= out
->ap_req_options
;
713 ret
= krb5_copy_ticket(context
, out
->ticket
, ticket
);
719 krb5_rd_req_out_ctx_free(context
, out
);
727 static krb5_error_code
728 get_key_from_keytab(krb5_context context
,
729 krb5_auth_context
*auth_context
,
731 krb5_const_principal server
,
733 krb5_keyblock
**out_key
)
735 krb5_keytab_entry entry
;
738 krb5_keytab real_keytab
;
741 krb5_kt_default(context
, &real_keytab
);
743 real_keytab
= keytab
;
745 if (ap_req
->ticket
.enc_part
.kvno
)
746 kvno
= *ap_req
->ticket
.enc_part
.kvno
;
750 ret
= krb5_kt_get_entry (context
,
754 ap_req
->ticket
.enc_part
.etype
,
758 ret
= krb5_copy_keyblock(context
, &entry
.keyblock
, out_key
);
759 krb5_kt_free_entry (context
, &entry
);
762 krb5_kt_close(context
, real_keytab
);
771 krb5_error_code KRB5_LIB_FUNCTION
772 krb5_rd_req_ctx(krb5_context context
,
773 krb5_auth_context
*auth_context
,
774 const krb5_data
*inbuf
,
775 krb5_const_principal server
,
776 krb5_rd_req_in_ctx inctx
,
777 krb5_rd_req_out_ctx
*outctx
)
781 krb5_principal service
= NULL
;
782 krb5_rd_req_out_ctx o
= NULL
;
784 ret
= _krb5_rd_req_out_ctx_alloc(context
, &o
);
788 if (*auth_context
== NULL
) {
789 ret
= krb5_auth_con_init(context
, auth_context
);
794 ret
= krb5_decode_ap_req(context
, inbuf
, &ap_req
);
799 ret
= _krb5_principalname2krb5_principal(context
,
802 ap_req
.ticket
.realm
);
807 if (ap_req
.ap_options
.use_session_key
&&
808 (*auth_context
)->keyblock
== NULL
) {
809 krb5_set_error_string(context
, "krb5_rd_req: user to user auth "
810 "without session key given");
811 ret
= KRB5KRB_AP_ERR_NOKEY
;
815 if((*auth_context
)->keyblock
){
816 ret
= krb5_copy_keyblock(context
,
817 (*auth_context
)->keyblock
,
821 } else if(inctx
->keyblock
){
822 ret
= krb5_copy_keyblock(context
,
828 krb5_keytab keytab
= NULL
;
830 if (inctx
&& inctx
->keytab
)
831 keytab
= inctx
->keytab
;
833 ret
= get_key_from_keytab(context
,
843 ret
= krb5_verify_ap_req2(context
,
851 KRB5_KU_AP_REQ_AUTH
);
856 /* If there is a PAC, verify its server signature */
857 if (inctx
->check_pac
) {
861 ret
= krb5_ticket_get_authorization_data_type(context
,
863 KRB5_AUTHDATA_WIN2K_PAC
,
866 ret
= krb5_pac_parse(context
, data
.data
, data
.length
, &pac
);
867 krb5_data_free(&data
);
871 ret
= krb5_pac_verify(context
,
873 o
->ticket
->ticket
.authtime
,
877 krb5_pac_free(context
, pac
);
884 if (ret
|| outctx
== NULL
) {
885 krb5_rd_req_out_ctx_free(context
, o
);
889 free_AP_REQ(&ap_req
);
891 krb5_free_principal(context
, service
);