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>
38 static krb5_error_code
39 decrypt_tkt_enc_part (krb5_context context
,
41 EncryptedData
*enc_part
,
42 EncTicketPart
*decr_part
)
49 ret
= krb5_crypto_init(context
, key
, 0, &crypto
);
52 ret
= krb5_decrypt_EncryptedData (context
,
57 krb5_crypto_destroy(context
, crypto
);
61 ret
= krb5_decode_EncTicketPart(context
, plain
.data
, plain
.length
,
63 krb5_data_free (&plain
);
67 static krb5_error_code
68 decrypt_authenticator (krb5_context context
,
70 EncryptedData
*enc_part
,
71 Authenticator
*authenticator
,
79 ret
= krb5_crypto_init(context
, key
, 0, &crypto
);
82 ret
= krb5_decrypt_EncryptedData (context
,
84 usage
/* KRB5_KU_AP_REQ_AUTH */,
87 /* for backwards compatibility, also try the old usage */
88 if (ret
&& usage
== KRB5_KU_TGS_REQ_AUTH
)
89 ret
= krb5_decrypt_EncryptedData (context
,
94 krb5_crypto_destroy(context
, crypto
);
98 ret
= krb5_decode_Authenticator(context
, plain
.data
, plain
.length
,
100 krb5_data_free (&plain
);
104 krb5_error_code KRB5_LIB_FUNCTION
105 krb5_decode_ap_req(krb5_context context
,
106 const krb5_data
*inbuf
,
111 ret
= decode_AP_REQ(inbuf
->data
, inbuf
->length
, ap_req
, &len
);
114 if (ap_req
->pvno
!= 5){
116 krb5_clear_error_string (context
);
117 return KRB5KRB_AP_ERR_BADVERSION
;
119 if (ap_req
->msg_type
!= krb_ap_req
){
121 krb5_clear_error_string (context
);
122 return KRB5KRB_AP_ERR_MSG_TYPE
;
124 if (ap_req
->ticket
.tkt_vno
!= 5){
126 krb5_clear_error_string (context
);
127 return KRB5KRB_AP_ERR_BADVERSION
;
132 static krb5_error_code
133 check_transited(krb5_context context
, Ticket
*ticket
, EncTicketPart
*enc
)
140 * Windows 2000 and 2003 uses this inside their TGT so it's normaly
141 * not seen by others, however, samba4 joined with a Windows AD as
142 * a Domain Controller gets exposed to this.
144 if(enc
->transited
.tr_type
== 0 && enc
->transited
.contents
.length
== 0)
147 if(enc
->transited
.tr_type
!= DOMAIN_X500_COMPRESS
)
148 return KRB5KDC_ERR_TRTYPE_NOSUPP
;
150 if(enc
->transited
.contents
.length
== 0)
153 ret
= krb5_domain_x500_decode(context
, enc
->transited
.contents
,
154 &realms
, &num_realms
,
159 ret
= krb5_check_transited(context
, enc
->crealm
,
161 realms
, num_realms
, NULL
);
166 static krb5_error_code
167 find_etypelist(krb5_context context
,
168 krb5_auth_context auth_context
,
173 krb5_authdata adIfRelevant
;
176 adIfRelevant
.len
= 0;
181 ad
= auth_context
->authenticator
->authorization_data
;
185 for (i
= 0; i
< ad
->len
; i
++) {
186 if (ad
->val
[i
].ad_type
== KRB5_AUTHDATA_IF_RELEVANT
) {
187 ret
= decode_AD_IF_RELEVANT(ad
->val
[i
].ad_data
.data
,
188 ad
->val
[i
].ad_data
.length
,
194 if (adIfRelevant
.len
== 1 &&
195 adIfRelevant
.val
[0].ad_type
==
196 KRB5_AUTHDATA_GSS_API_ETYPE_NEGOTIATION
) {
199 free_AD_IF_RELEVANT(&adIfRelevant
);
200 adIfRelevant
.len
= 0;
204 if (adIfRelevant
.len
== 0)
207 ret
= decode_EtypeList(adIfRelevant
.val
[0].ad_data
.data
,
208 adIfRelevant
.val
[0].ad_data
.length
,
212 krb5_clear_error_string(context
);
214 free_AD_IF_RELEVANT(&adIfRelevant
);
219 krb5_error_code KRB5_LIB_FUNCTION
220 krb5_decrypt_ticket(krb5_context context
,
228 ret
= decrypt_tkt_enc_part (context
, key
, &ticket
->enc_part
, &t
);
234 time_t start
= t
.authtime
;
236 krb5_timeofday (context
, &now
);
238 start
= *t
.starttime
;
239 if(start
- now
> context
->max_skew
241 && !(flags
& KRB5_VERIFY_AP_REQ_IGNORE_INVALID
))) {
242 free_EncTicketPart(&t
);
243 krb5_clear_error_string (context
);
244 return KRB5KRB_AP_ERR_TKT_NYV
;
246 if(now
- t
.endtime
> context
->max_skew
) {
247 free_EncTicketPart(&t
);
248 krb5_clear_error_string (context
);
249 return KRB5KRB_AP_ERR_TKT_EXPIRED
;
252 if(!t
.flags
.transited_policy_checked
) {
253 ret
= check_transited(context
, ticket
, &t
);
255 free_EncTicketPart(&t
);
264 free_EncTicketPart(&t
);
268 krb5_error_code KRB5_LIB_FUNCTION
269 krb5_verify_authenticator_checksum(krb5_context context
,
270 krb5_auth_context ac
,
276 krb5_authenticator authenticator
;
279 ret
= krb5_auth_con_getauthenticator (context
,
284 if(authenticator
->cksum
== NULL
) {
285 krb5_free_authenticator(context
, &authenticator
);
288 ret
= krb5_auth_con_getkey(context
, ac
, &key
);
290 krb5_free_authenticator(context
, &authenticator
);
293 ret
= krb5_crypto_init(context
, key
, 0, &crypto
);
296 ret
= krb5_verify_checksum (context
,
298 KRB5_KU_AP_REQ_AUTH_CKSUM
,
301 authenticator
->cksum
);
302 krb5_crypto_destroy(context
, crypto
);
304 krb5_free_authenticator(context
, &authenticator
);
305 krb5_free_keyblock(context
, key
);
310 krb5_error_code KRB5_LIB_FUNCTION
311 krb5_verify_ap_req(krb5_context context
,
312 krb5_auth_context
*auth_context
,
314 krb5_const_principal server
,
315 krb5_keyblock
*keyblock
,
317 krb5_flags
*ap_req_options
,
318 krb5_ticket
**ticket
)
320 return krb5_verify_ap_req2 (context
,
328 KRB5_KU_AP_REQ_AUTH
);
331 krb5_error_code KRB5_LIB_FUNCTION
332 krb5_verify_ap_req2(krb5_context context
,
333 krb5_auth_context
*auth_context
,
335 krb5_const_principal server
,
336 krb5_keyblock
*keyblock
,
338 krb5_flags
*ap_req_options
,
339 krb5_ticket
**ticket
,
340 krb5_key_usage usage
)
343 krb5_auth_context ac
;
350 if (auth_context
&& *auth_context
) {
353 ret
= krb5_auth_con_init (context
, &ac
);
358 t
= calloc(1, sizeof(*t
));
361 krb5_clear_error_string (context
);
365 if (ap_req
->ap_options
.use_session_key
&& ac
->keyblock
){
366 ret
= krb5_decrypt_ticket(context
, &ap_req
->ticket
,
370 krb5_free_keyblock(context
, ac
->keyblock
);
373 ret
= krb5_decrypt_ticket(context
, &ap_req
->ticket
,
381 ret
= _krb5_principalname2krb5_principal(context
,
383 ap_req
->ticket
.sname
,
384 ap_req
->ticket
.realm
);
386 ret
= _krb5_principalname2krb5_principal(context
,
394 ret
= krb5_copy_keyblock(context
, &t
->ticket
.key
, &ac
->keyblock
);
397 ret
= decrypt_authenticator (context
,
399 &ap_req
->authenticator
,
406 krb5_principal p1
, p2
;
409 _krb5_principalname2krb5_principal(context
,
411 ac
->authenticator
->cname
,
412 ac
->authenticator
->crealm
);
413 _krb5_principalname2krb5_principal(context
,
417 res
= krb5_principal_compare (context
, p1
, p2
);
418 krb5_free_principal (context
, p1
);
419 krb5_free_principal (context
, p2
);
421 ret
= KRB5KRB_AP_ERR_BADMATCH
;
422 krb5_clear_error_string (context
);
427 /* check addresses */
430 && ac
->remote_address
431 && !krb5_address_search (context
,
434 ret
= KRB5KRB_AP_ERR_BADADDR
;
435 krb5_clear_error_string (context
);
439 /* check timestamp in authenticator */
443 krb5_timeofday (context
, &now
);
445 if (abs(ac
->authenticator
->ctime
- now
) > context
->max_skew
) {
446 ret
= KRB5KRB_AP_ERR_SKEW
;
447 krb5_clear_error_string (context
);
452 if (ac
->authenticator
->seq_number
)
453 krb5_auth_con_setremoteseqnumber(context
, ac
,
454 *ac
->authenticator
->seq_number
);
456 /* XXX - Xor sequence numbers */
458 if (ac
->authenticator
->subkey
) {
459 ret
= krb5_auth_con_setremotesubkey(context
, ac
,
460 ac
->authenticator
->subkey
);
465 ret
= find_etypelist(context
, ac
, &etypes
);
469 ac
->keytype
= ETYPE_NULL
;
474 for (i
= 0; i
< etypes
.len
; i
++) {
475 if (krb5_enctype_valid(context
, etypes
.val
[i
]) == 0) {
476 ac
->keytype
= etypes
.val
[i
];
482 if (ap_req_options
) {
484 if (ac
->keytype
!= ETYPE_NULL
)
485 *ap_req_options
|= AP_OPTS_USE_SUBKEY
;
486 if (ap_req
->ap_options
.use_session_key
)
487 *ap_req_options
|= AP_OPTS_USE_SESSION_KEY
;
488 if (ap_req
->ap_options
.mutual_required
)
489 *ap_req_options
|= AP_OPTS_MUTUAL_REQUIRED
;
495 krb5_free_ticket (context
, t
);
497 if (*auth_context
== NULL
)
500 krb5_auth_con_free (context
, ac
);
501 free_EtypeList(&etypes
);
505 krb5_free_ticket (context
, t
);
506 if (auth_context
== NULL
|| *auth_context
== NULL
)
507 krb5_auth_con_free (context
, ac
);
515 struct krb5_rd_req_in_ctx_data
{
517 krb5_keyblock
*keyblock
;
518 krb5_boolean check_pac
;
521 struct krb5_rd_req_out_ctx_data
{
522 krb5_keyblock
*keyblock
;
523 krb5_flags ap_req_options
;
531 krb5_error_code KRB5_LIB_FUNCTION
532 krb5_rd_req_in_ctx_alloc(krb5_context context
, krb5_rd_req_in_ctx
*ctx
)
534 *ctx
= calloc(1, sizeof(**ctx
));
536 krb5_set_error_string(context
, "out of memory");
539 (*ctx
)->check_pac
= (context
->flags
& KRB5_CTX_F_CHECK_PAC
) ? 1 : 0;
543 krb5_error_code KRB5_LIB_FUNCTION
544 krb5_rd_req_in_set_keytab(krb5_context context
,
545 krb5_rd_req_in_ctx in
,
548 in
->keytab
= keytab
; /* XXX should make copy */
553 * Set if krb5_rq_red() is going to check the Windows PAC or not
555 * @param context Keberos 5 context.
556 * @param in krb5_rd_req_in_ctx to check the option on.
557 * @param flag flag to select if to check the pac (TRUE) or not (FALSE).
559 * @return Kerberos 5 error code, see krb5_get_error_message().
564 krb5_error_code KRB5_LIB_FUNCTION
565 krb5_rd_req_in_set_pac_check(krb5_context context
,
566 krb5_rd_req_in_ctx in
,
569 in
->check_pac
= flag
;
574 krb5_error_code KRB5_LIB_FUNCTION
575 krb5_rd_req_in_set_keyblock(krb5_context context
,
576 krb5_rd_req_in_ctx in
,
577 krb5_keyblock
*keyblock
)
579 in
->keyblock
= keyblock
; /* XXX should make copy */
583 krb5_error_code KRB5_LIB_FUNCTION
584 krb5_rd_req_out_get_ap_req_options(krb5_context context
,
585 krb5_rd_req_out_ctx out
,
586 krb5_flags
*ap_req_options
)
588 *ap_req_options
= out
->ap_req_options
;
592 krb5_error_code KRB5_LIB_FUNCTION
593 krb5_rd_req_out_get_ticket(krb5_context context
,
594 krb5_rd_req_out_ctx out
,
595 krb5_ticket
**ticket
)
597 return krb5_copy_ticket(context
, out
->ticket
, ticket
);
600 krb5_error_code KRB5_LIB_FUNCTION
601 krb5_rd_req_out_get_keyblock(krb5_context context
,
602 krb5_rd_req_out_ctx out
,
603 krb5_keyblock
**keyblock
)
605 return krb5_copy_keyblock(context
, out
->keyblock
, keyblock
);
608 void KRB5_LIB_FUNCTION
609 krb5_rd_req_in_ctx_free(krb5_context context
, krb5_rd_req_in_ctx ctx
)
614 krb5_error_code KRB5_LIB_FUNCTION
615 _krb5_rd_req_out_ctx_alloc(krb5_context context
, krb5_rd_req_out_ctx
*ctx
)
617 *ctx
= calloc(1, sizeof(**ctx
));
619 krb5_set_error_string(context
, "out of memory");
625 void KRB5_LIB_FUNCTION
626 krb5_rd_req_out_ctx_free(krb5_context context
, krb5_rd_req_out_ctx ctx
)
628 krb5_free_keyblock(context
, ctx
->keyblock
);
636 krb5_error_code KRB5_LIB_FUNCTION
637 krb5_rd_req(krb5_context context
,
638 krb5_auth_context
*auth_context
,
639 const krb5_data
*inbuf
,
640 krb5_const_principal server
,
642 krb5_flags
*ap_req_options
,
643 krb5_ticket
**ticket
)
646 krb5_rd_req_in_ctx in
;
647 krb5_rd_req_out_ctx out
;
649 ret
= krb5_rd_req_in_ctx_alloc(context
, &in
);
653 ret
= krb5_rd_req_in_set_keytab(context
, in
, keytab
);
655 krb5_rd_req_in_ctx_free(context
, in
);
659 ret
= krb5_rd_req_ctx(context
, auth_context
, inbuf
, server
, in
, &out
);
660 krb5_rd_req_in_ctx_free(context
, in
);
665 *ap_req_options
= out
->ap_req_options
;
667 ret
= krb5_copy_ticket(context
, out
->ticket
, ticket
);
673 krb5_rd_req_out_ctx_free(context
, out
);
681 krb5_error_code KRB5_LIB_FUNCTION
682 krb5_rd_req_with_keyblock(krb5_context context
,
683 krb5_auth_context
*auth_context
,
684 const krb5_data
*inbuf
,
685 krb5_const_principal server
,
686 krb5_keyblock
*keyblock
,
687 krb5_flags
*ap_req_options
,
688 krb5_ticket
**ticket
)
691 krb5_rd_req_in_ctx in
;
692 krb5_rd_req_out_ctx out
;
694 ret
= krb5_rd_req_in_ctx_alloc(context
, &in
);
698 ret
= krb5_rd_req_in_set_keyblock(context
, in
, keyblock
);
700 krb5_rd_req_in_ctx_free(context
, in
);
704 ret
= krb5_rd_req_ctx(context
, auth_context
, inbuf
, server
, in
, &out
);
705 krb5_rd_req_in_ctx_free(context
, in
);
710 *ap_req_options
= out
->ap_req_options
;
712 ret
= krb5_copy_ticket(context
, out
->ticket
, ticket
);
718 krb5_rd_req_out_ctx_free(context
, out
);
726 static krb5_error_code
727 get_key_from_keytab(krb5_context context
,
728 krb5_auth_context
*auth_context
,
730 krb5_const_principal server
,
732 krb5_keyblock
**out_key
)
734 krb5_keytab_entry entry
;
737 krb5_keytab real_keytab
;
740 krb5_kt_default(context
, &real_keytab
);
742 real_keytab
= keytab
;
744 if (ap_req
->ticket
.enc_part
.kvno
)
745 kvno
= *ap_req
->ticket
.enc_part
.kvno
;
749 ret
= krb5_kt_get_entry (context
,
753 ap_req
->ticket
.enc_part
.etype
,
757 ret
= krb5_copy_keyblock(context
, &entry
.keyblock
, out_key
);
758 krb5_kt_free_entry (context
, &entry
);
761 krb5_kt_close(context
, real_keytab
);
770 krb5_error_code KRB5_LIB_FUNCTION
771 krb5_rd_req_ctx(krb5_context context
,
772 krb5_auth_context
*auth_context
,
773 const krb5_data
*inbuf
,
774 krb5_const_principal server
,
775 krb5_rd_req_in_ctx inctx
,
776 krb5_rd_req_out_ctx
*outctx
)
780 krb5_principal service
= NULL
;
781 krb5_rd_req_out_ctx o
= NULL
;
783 ret
= _krb5_rd_req_out_ctx_alloc(context
, &o
);
787 if (*auth_context
== NULL
) {
788 ret
= krb5_auth_con_init(context
, auth_context
);
793 ret
= krb5_decode_ap_req(context
, inbuf
, &ap_req
);
798 ret
= _krb5_principalname2krb5_principal(context
,
801 ap_req
.ticket
.realm
);
806 if (ap_req
.ap_options
.use_session_key
&&
807 (*auth_context
)->keyblock
== NULL
) {
808 krb5_set_error_string(context
, "krb5_rd_req: user to user auth "
809 "without session key given");
810 ret
= KRB5KRB_AP_ERR_NOKEY
;
814 if((*auth_context
)->keyblock
){
815 ret
= krb5_copy_keyblock(context
,
816 (*auth_context
)->keyblock
,
820 } else if(inctx
->keyblock
){
821 ret
= krb5_copy_keyblock(context
,
827 krb5_keytab keytab
= NULL
;
829 if (inctx
&& inctx
->keytab
)
830 keytab
= inctx
->keytab
;
832 ret
= get_key_from_keytab(context
,
842 ret
= krb5_verify_ap_req2(context
,
850 KRB5_KU_AP_REQ_AUTH
);
855 /* If there is a PAC, verify its server signature */
856 if (inctx
->check_pac
) {
860 ret
= krb5_ticket_get_authorization_data_type(context
,
862 KRB5_AUTHDATA_WIN2K_PAC
,
865 ret
= krb5_pac_parse(context
, data
.data
, data
.length
, &pac
);
866 krb5_data_free(&data
);
870 ret
= krb5_pac_verify(context
,
872 o
->ticket
->ticket
.authtime
,
876 krb5_pac_free(context
, pac
);
883 if (ret
|| outctx
== NULL
) {
884 krb5_rd_req_out_ctx_free(context
, o
);
888 free_AP_REQ(&ap_req
);
890 krb5_free_principal(context
, service
);