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 static krb5_error_code
37 decrypt_tkt_enc_part (krb5_context context,
39 EncryptedData *enc_part,
40 EncTicketPart *decr_part)
47 ret = krb5_crypto_init(context, key, 0, &crypto);
50 ret = krb5_decrypt_EncryptedData (context,
55 krb5_crypto_destroy(context, crypto);
59 ret = decode_EncTicketPart(plain.data, plain.length, decr_part, &len);
61 krb5_set_error_message(context, ret,
62 N_("Failed to decode encrypted "
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 = decode_Authenticator(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_message (context);
118 return KRB5KRB_AP_ERR_BADVERSION;
120 if (ap_req->msg_type != krb_ap_req){
122 krb5_clear_error_message (context);
123 return KRB5KRB_AP_ERR_MSG_TYPE;
125 if (ap_req->ticket.tkt_vno != 5){
127 krb5_clear_error_message (context);
128 return KRB5KRB_AP_ERR_BADVERSION;
133 static krb5_error_code
134 check_transited(krb5_context context, Ticket *ticket, EncTicketPart *enc)
137 unsigned int num_realms;
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_message(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_message (context);
245 return KRB5KRB_AP_ERR_TKT_NYV;
247 if(now - t.endtime > context->max_skew) {
248 free_EncTicketPart(&t);
249 krb5_clear_error_message (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_message (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,
393 ret = decrypt_authenticator (context,
395 &ap_req->authenticator,
402 krb5_principal p1, p2;
405 _krb5_principalname2krb5_principal(context,
407 ac->authenticator->cname,
408 ac->authenticator->crealm);
409 _krb5_principalname2krb5_principal(context,
413 res = krb5_principal_compare (context, p1, p2);
414 krb5_free_principal (context, p1);
415 krb5_free_principal (context, p2);
417 ret = KRB5KRB_AP_ERR_BADMATCH;
418 krb5_clear_error_message (context);
423 /* check addresses */
426 && ac->remote_address
427 && !krb5_address_search (context,
430 ret = KRB5KRB_AP_ERR_BADADDR;
431 krb5_clear_error_message (context);
435 /* check timestamp in authenticator */
439 krb5_timeofday (context, &now);
441 if (abs(ac->authenticator->ctime - now) > context->max_skew) {
442 ret = KRB5KRB_AP_ERR_SKEW;
443 krb5_clear_error_message (context);
448 if (ac->authenticator->seq_number)
449 krb5_auth_con_setremoteseqnumber(context, ac,
450 *ac->authenticator->seq_number);
452 /* XXX - Xor sequence numbers */
454 if (ac->authenticator->subkey) {
455 ret = krb5_auth_con_setremotesubkey(context, ac,
456 ac->authenticator->subkey);
461 ret = find_etypelist(context, ac, &etypes);
465 ac->keytype = ETYPE_NULL;
470 for (i = 0; i < etypes.len; i++) {
471 if (krb5_enctype_valid(context, etypes.val[i]) == 0) {
472 ac->keytype = etypes.val[i];
479 ret = krb5_copy_keyblock(context, &t->ticket.key, &ac->keyblock);
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;
525 krb5_principal server;
529 * Allocate a krb5_rd_req_in_ctx as an input parameter to
530 * krb5_rd_req_ctx(). The caller should free the context with
531 * krb5_rd_req_in_ctx_free() when done with the context.
533 * @param context Keberos 5 context.
534 * @param ctx in ctx to krb5_rd_req_ctx().
536 * @return Kerberos 5 error code, see krb5_get_error_message().
541 krb5_error_code KRB5_LIB_FUNCTION
542 krb5_rd_req_in_ctx_alloc(krb5_context context, krb5_rd_req_in_ctx *ctx)
544 *ctx = calloc(1, sizeof(**ctx));
546 krb5_set_error_message(context, ENOMEM,
547 N_("malloc: out of memory", ""));
550 (*ctx)->check_pac = (context->flags & KRB5_CTX_F_CHECK_PAC) ? 1 : 0;
555 * Set the keytab that krb5_rd_req_ctx() will use.
557 * @param context Keberos 5 context.
558 * @param in in ctx to krb5_rd_req_ctx().
559 * @param keytab keytab that krb5_rd_req_ctx() will use, only copy the
560 * pointer, so the caller must free they keytab after
561 * krb5_rd_req_in_ctx_free() is called.
563 * @return Kerberos 5 error code, see krb5_get_error_message().
568 krb5_error_code KRB5_LIB_FUNCTION
569 krb5_rd_req_in_set_keytab(krb5_context context,
570 krb5_rd_req_in_ctx in,
578 * Set if krb5_rq_red() is going to check the Windows PAC or not
580 * @param context Keberos 5 context.
581 * @param in krb5_rd_req_in_ctx to check the option on.
582 * @param flag flag to select if to check the pac (TRUE) or not (FALSE).
584 * @return Kerberos 5 error code, see krb5_get_error_message().
589 krb5_error_code KRB5_LIB_FUNCTION
590 krb5_rd_req_in_set_pac_check(krb5_context context,
591 krb5_rd_req_in_ctx in,
594 in->check_pac = flag;
599 krb5_error_code KRB5_LIB_FUNCTION
600 krb5_rd_req_in_set_keyblock(krb5_context context,
601 krb5_rd_req_in_ctx in,
602 krb5_keyblock *keyblock)
604 in->keyblock = keyblock; /* XXX should make copy */
608 krb5_error_code KRB5_LIB_FUNCTION
609 krb5_rd_req_out_get_ap_req_options(krb5_context context,
610 krb5_rd_req_out_ctx out,
611 krb5_flags *ap_req_options)
613 *ap_req_options = out->ap_req_options;
617 krb5_error_code KRB5_LIB_FUNCTION
618 krb5_rd_req_out_get_ticket(krb5_context context,
619 krb5_rd_req_out_ctx out,
620 krb5_ticket **ticket)
622 return krb5_copy_ticket(context, out->ticket, ticket);
625 krb5_error_code KRB5_LIB_FUNCTION
626 krb5_rd_req_out_get_keyblock(krb5_context context,
627 krb5_rd_req_out_ctx out,
628 krb5_keyblock **keyblock)
630 return krb5_copy_keyblock(context, out->keyblock, keyblock);
634 * Get the principal that was used in the request from the
635 * client. Might not match whats in the ticket if krb5_rd_req_ctx()
636 * searched in the keytab for a matching key.
638 * @param context a Kerberos 5 context.
639 * @param out a krb5_rd_req_out_ctx from krb5_rd_req_ctx().
640 * @param principal return principal, free with krb5_free_principal().
645 krb5_error_code KRB5_LIB_FUNCTION
646 krb5_rd_req_out_get_server(krb5_context context,
647 krb5_rd_req_out_ctx out,
648 krb5_principal *principal)
650 return krb5_copy_principal(context, out->server, principal);
653 void KRB5_LIB_FUNCTION
654 krb5_rd_req_in_ctx_free(krb5_context context, krb5_rd_req_in_ctx ctx)
660 * Free the krb5_rd_req_out_ctx.
662 * @param context Keberos 5 context.
663 * @param ctx krb5_rd_req_out_ctx context to free.
668 void KRB5_LIB_FUNCTION
669 krb5_rd_req_out_ctx_free(krb5_context context, krb5_rd_req_out_ctx ctx)
672 krb5_free_ticket(context, ctx->ticket);
674 krb5_free_keyblock(context, ctx->keyblock);
676 krb5_free_principal(context, ctx->server);
684 krb5_error_code KRB5_LIB_FUNCTION
685 krb5_rd_req(krb5_context context,
686 krb5_auth_context *auth_context,
687 const krb5_data *inbuf,
688 krb5_const_principal server,
690 krb5_flags *ap_req_options,
691 krb5_ticket **ticket)
694 krb5_rd_req_in_ctx in;
695 krb5_rd_req_out_ctx out;
697 ret = krb5_rd_req_in_ctx_alloc(context, &in);
701 ret = krb5_rd_req_in_set_keytab(context, in, keytab);
703 krb5_rd_req_in_ctx_free(context, in);
707 ret = krb5_rd_req_ctx(context, auth_context, inbuf, server, in, &out);
708 krb5_rd_req_in_ctx_free(context, in);
713 *ap_req_options = out->ap_req_options;
715 ret = krb5_copy_ticket(context, out->ticket, ticket);
721 krb5_rd_req_out_ctx_free(context, out);
729 krb5_error_code KRB5_LIB_FUNCTION
730 krb5_rd_req_with_keyblock(krb5_context context,
731 krb5_auth_context *auth_context,
732 const krb5_data *inbuf,
733 krb5_const_principal server,
734 krb5_keyblock *keyblock,
735 krb5_flags *ap_req_options,
736 krb5_ticket **ticket)
739 krb5_rd_req_in_ctx in;
740 krb5_rd_req_out_ctx out;
742 ret = krb5_rd_req_in_ctx_alloc(context, &in);
746 ret = krb5_rd_req_in_set_keyblock(context, in, keyblock);
748 krb5_rd_req_in_ctx_free(context, in);
752 ret = krb5_rd_req_ctx(context, auth_context, inbuf, server, in, &out);
753 krb5_rd_req_in_ctx_free(context, in);
758 *ap_req_options = out->ap_req_options;
760 ret = krb5_copy_ticket(context, out->ticket, ticket);
766 krb5_rd_req_out_ctx_free(context, out);
774 static krb5_error_code
775 get_key_from_keytab(krb5_context context,
777 krb5_const_principal server,
779 krb5_keyblock **out_key)
781 krb5_keytab_entry entry;
784 krb5_keytab real_keytab;
787 krb5_kt_default(context, &real_keytab);
789 real_keytab = keytab;
791 if (ap_req->ticket.enc_part.kvno)
792 kvno = *ap_req->ticket.enc_part.kvno;
796 ret = krb5_kt_get_entry (context,
800 ap_req->ticket.enc_part.etype,
804 ret = krb5_copy_keyblock(context, &entry.keyblock, out_key);
805 krb5_kt_free_entry (context, &entry);
808 krb5_kt_close(context, real_keytab);
814 * The core server function that verify application authentication
815 * requests from clients.
817 * @param context Keberos 5 context.
818 * @param auth_context the authentication context, can be NULL, then
819 * default values for the authentication context will used.
820 * @param inbuf the (AP-REQ) authentication buffer
822 * @param server the server with authenticate as, if NULL the function
823 * will try to find any available credential in the keytab
824 * that will verify the reply. The function will prefer the
825 * server the server client specified in the AP-REQ, but if
826 * there is no mach, it will try all keytab entries for a
827 * match. This have serious performance issues for larger keytabs.
829 * @param inctx control the behavior of the function, if NULL, the
830 * default behavior is used.
831 * @param outctx the return outctx, free with krb5_rd_req_out_ctx_free().
832 * @return Kerberos 5 error code, see krb5_get_error_message().
837 krb5_error_code KRB5_LIB_FUNCTION
838 krb5_rd_req_ctx(krb5_context context,
839 krb5_auth_context *auth_context,
840 const krb5_data *inbuf,
841 krb5_const_principal server,
842 krb5_rd_req_in_ctx inctx,
843 krb5_rd_req_out_ctx *outctx)
847 krb5_rd_req_out_ctx o = NULL;
848 krb5_keytab id = NULL, keytab = NULL;
849 krb5_principal service = NULL;
853 o = calloc(1, sizeof(*o));
855 krb5_set_error_message(context, ENOMEM,
856 N_("malloc: out of memory", ""));
860 if (*auth_context == NULL) {
861 ret = krb5_auth_con_init(context, auth_context);
866 ret = krb5_decode_ap_req(context, inbuf, &ap_req);
870 /* Save that principal that was in the request */
871 ret = _krb5_principalname2krb5_principal(context,
874 ap_req.ticket.realm);
878 if (ap_req.ap_options.use_session_key &&
879 (*auth_context)->keyblock == NULL) {
880 ret = KRB5KRB_AP_ERR_NOKEY;
881 krb5_set_error_message(context, ret,
882 N_("krb5_rd_req: user to user auth "
883 "without session key given", ""));
887 if (inctx && inctx->keytab)
890 if((*auth_context)->keyblock){
891 ret = krb5_copy_keyblock(context,
892 (*auth_context)->keyblock,
896 } else if(inctx && inctx->keyblock){
897 ret = krb5_copy_keyblock(context,
905 krb5_kt_default(context, &keytab);
911 if (server == NULL) {
912 ret = _krb5_principalname2krb5_principal(context,
915 ap_req.ticket.realm);
921 ret = get_key_from_keytab(context,
927 /* If caller specified a server, fail. */
930 /* Otherwise, fall back to iterating over the keytab. This
931 * have serious performace issues for larger keytab.
939 * We got an exact keymatch, use that.
942 ret = krb5_verify_ap_req2(context,
950 KRB5_KU_AP_REQ_AUTH);
957 * Interate over keytab to find a key that can decrypt the request.
960 krb5_keytab_entry entry;
961 krb5_kt_cursor cursor;
962 int done = 0, kvno = 0;
964 memset(&cursor, 0, sizeof(cursor));
966 if (ap_req.ticket.enc_part.kvno)
967 kvno = *ap_req.ticket.enc_part.kvno;
969 ret = krb5_kt_start_seq_get(context, id, &cursor);
977 ret = krb5_kt_next_entry(context, id, &entry, &cursor);
979 _krb5_kt_principal_not_found(context, ret, id, o->server,
980 ap_req.ticket.enc_part.etype,
985 if (entry.keyblock.keytype != ap_req.ticket.enc_part.etype ||
986 (kvno && kvno != entry.vno)) {
987 krb5_kt_free_entry (context, &entry);
991 ret = krb5_verify_ap_req2(context,
999 KRB5_KU_AP_REQ_AUTH);
1001 krb5_kt_free_entry (context, &entry);
1006 * Found a match, save the keyblock for PAC processing,
1007 * and update the service principal in the ticket to match
1008 * whatever is in the keytab.
1011 ret = krb5_copy_keyblock(context,
1015 krb5_kt_free_entry (context, &entry);
1019 ret = krb5_copy_principal(context, entry.principal, &p);
1021 krb5_kt_free_entry (context, &entry);
1024 krb5_free_principal(context, o->ticket->server);
1025 o->ticket->server = p;
1027 krb5_kt_free_entry (context, &entry);
1031 krb5_kt_end_seq_get (context, id, &cursor);
1034 /* If there is a PAC, verify its server signature */
1035 if (inctx == NULL || inctx->check_pac) {
1039 ret = krb5_ticket_get_authorization_data_type(context,
1041 KRB5_AUTHDATA_WIN2K_PAC,
1044 ret = krb5_pac_parse(context, data.data, data.length, &pac);
1045 krb5_data_free(&data);
1049 ret = krb5_pac_verify(context,
1051 o->ticket->ticket.authtime,
1055 krb5_pac_free(context, pac);
1063 if (ret || outctx == NULL) {
1064 krb5_rd_req_out_ctx_free(context, o);
1068 free_AP_REQ(&ap_req);
1071 krb5_free_principal(context, service);
1074 krb5_kt_close(context, keytab);