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,
177 krb5_authdata adIfRelevant;
180 memset(&adIfRelevant, 0, sizeof(adIfRelevant));
185 ad = auth_context->authenticator->authorization_data;
189 for (i = 0; i < ad->len; i++) {
190 if (ad->val[i].ad_type == KRB5_AUTHDATA_IF_RELEVANT) {
191 ret = decode_AD_IF_RELEVANT(ad->val[i].ad_data.data,
192 ad->val[i].ad_data.length,
198 if (adIfRelevant.len == 1 &&
199 adIfRelevant.val[0].ad_type ==
200 KRB5_AUTHDATA_GSS_API_ETYPE_NEGOTIATION) {
203 free_AD_IF_RELEVANT(&adIfRelevant);
204 adIfRelevant.len = 0;
208 if (adIfRelevant.len == 0)
211 ret = decode_EtypeList(adIfRelevant.val[0].ad_data.data,
212 adIfRelevant.val[0].ad_data.length,
216 krb5_clear_error_message(context);
218 free_AD_IF_RELEVANT(&adIfRelevant);
223 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
224 krb5_decrypt_ticket(krb5_context context,
232 ret = decrypt_tkt_enc_part (context, key, &ticket->enc_part, &t);
238 time_t start = t.authtime;
240 krb5_timeofday (context, &now);
242 start = *t.starttime;
243 if(start - now > context->max_skew
245 && !(flags & KRB5_VERIFY_AP_REQ_IGNORE_INVALID))) {
246 free_EncTicketPart(&t);
247 krb5_clear_error_message (context);
248 return KRB5KRB_AP_ERR_TKT_NYV;
250 if(now - t.endtime > context->max_skew) {
251 free_EncTicketPart(&t);
252 krb5_clear_error_message (context);
253 return KRB5KRB_AP_ERR_TKT_EXPIRED;
256 if(!t.flags.transited_policy_checked) {
257 ret = check_transited(context, ticket, &t);
259 free_EncTicketPart(&t);
268 free_EncTicketPart(&t);
272 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
273 krb5_verify_authenticator_checksum(krb5_context context,
274 krb5_auth_context ac,
280 krb5_authenticator authenticator;
283 ret = krb5_auth_con_getauthenticator (context,
288 if(authenticator->cksum == NULL) {
289 krb5_free_authenticator(context, &authenticator);
292 ret = krb5_auth_con_getkey(context, ac, &key);
294 krb5_free_authenticator(context, &authenticator);
297 ret = krb5_crypto_init(context, key, 0, &crypto);
300 ret = krb5_verify_checksum (context,
302 KRB5_KU_AP_REQ_AUTH_CKSUM,
305 authenticator->cksum);
306 krb5_crypto_destroy(context, crypto);
308 krb5_free_authenticator(context, &authenticator);
309 krb5_free_keyblock(context, key);
314 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
315 krb5_verify_ap_req(krb5_context context,
316 krb5_auth_context *auth_context,
318 krb5_const_principal server,
319 krb5_keyblock *keyblock,
321 krb5_flags *ap_req_options,
322 krb5_ticket **ticket)
324 return krb5_verify_ap_req2 (context,
332 KRB5_KU_AP_REQ_AUTH);
335 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
336 krb5_verify_ap_req2(krb5_context context,
337 krb5_auth_context *auth_context,
339 krb5_const_principal server,
340 krb5_keyblock *keyblock,
342 krb5_flags *ap_req_options,
343 krb5_ticket **ticket,
344 krb5_key_usage usage)
347 krb5_auth_context ac;
354 if (auth_context && *auth_context) {
357 ret = krb5_auth_con_init (context, &ac);
362 t = calloc(1, sizeof(*t));
365 krb5_clear_error_message (context);
369 if (ap_req->ap_options.use_session_key && ac->keyblock){
370 ret = krb5_decrypt_ticket(context, &ap_req->ticket,
374 krb5_free_keyblock(context, ac->keyblock);
377 ret = krb5_decrypt_ticket(context, &ap_req->ticket,
385 ret = _krb5_principalname2krb5_principal(context,
387 ap_req->ticket.sname,
388 ap_req->ticket.realm);
390 ret = _krb5_principalname2krb5_principal(context,
396 ret = decrypt_authenticator (context,
398 &ap_req->authenticator,
405 krb5_principal p1, p2;
408 _krb5_principalname2krb5_principal(context,
410 ac->authenticator->cname,
411 ac->authenticator->crealm);
412 _krb5_principalname2krb5_principal(context,
416 res = krb5_principal_compare (context, p1, p2);
417 krb5_free_principal (context, p1);
418 krb5_free_principal (context, p2);
420 ret = KRB5KRB_AP_ERR_BADMATCH;
421 krb5_clear_error_message (context);
426 /* check addresses */
429 && ac->remote_address
430 && !krb5_address_search (context,
433 ret = KRB5KRB_AP_ERR_BADADDR;
434 krb5_clear_error_message (context);
438 /* check timestamp in authenticator */
442 krb5_timeofday (context, &now);
444 if (abs(ac->authenticator->ctime - now) > context->max_skew) {
445 ret = KRB5KRB_AP_ERR_SKEW;
446 krb5_clear_error_message (context);
451 if (ac->authenticator->seq_number)
452 krb5_auth_con_setremoteseqnumber(context, ac,
453 *ac->authenticator->seq_number);
455 /* XXX - Xor sequence numbers */
457 if (ac->authenticator->subkey) {
458 ret = krb5_auth_con_setremotesubkey(context, ac,
459 ac->authenticator->subkey);
464 ret = find_etypelist(context, ac, &etypes);
468 ac->keytype = ETYPE_NULL;
473 for (i = 0; i < etypes.len; i++) {
474 if (krb5_enctype_valid(context, etypes.val[i]) == 0) {
475 ac->keytype = etypes.val[i];
482 ret = krb5_copy_keyblock(context, &t->ticket.key, &ac->keyblock);
485 if (ap_req_options) {
487 if (ac->keytype != ETYPE_NULL)
488 *ap_req_options |= AP_OPTS_USE_SUBKEY;
489 if (ap_req->ap_options.use_session_key)
490 *ap_req_options |= AP_OPTS_USE_SESSION_KEY;
491 if (ap_req->ap_options.mutual_required)
492 *ap_req_options |= AP_OPTS_MUTUAL_REQUIRED;
498 krb5_free_ticket (context, t);
500 if (*auth_context == NULL)
503 krb5_auth_con_free (context, ac);
504 free_EtypeList(&etypes);
508 krb5_free_ticket (context, t);
509 if (auth_context == NULL || *auth_context == NULL)
510 krb5_auth_con_free (context, ac);
518 struct krb5_rd_req_in_ctx_data {
520 krb5_keyblock *keyblock;
521 krb5_boolean check_pac;
524 struct krb5_rd_req_out_ctx_data {
525 krb5_keyblock *keyblock;
526 krb5_flags ap_req_options;
528 krb5_principal server;
532 * Allocate a krb5_rd_req_in_ctx as an input parameter to
533 * krb5_rd_req_ctx(). The caller should free the context with
534 * krb5_rd_req_in_ctx_free() when done with the context.
536 * @param context Keberos 5 context.
537 * @param ctx in ctx to krb5_rd_req_ctx().
539 * @return Kerberos 5 error code, see krb5_get_error_message().
544 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
545 krb5_rd_req_in_ctx_alloc(krb5_context context, krb5_rd_req_in_ctx *ctx)
547 *ctx = calloc(1, sizeof(**ctx));
549 krb5_set_error_message(context, ENOMEM,
550 N_("malloc: out of memory", ""));
553 (*ctx)->check_pac = (context->flags & KRB5_CTX_F_CHECK_PAC) ? 1 : 0;
558 * Set the keytab that krb5_rd_req_ctx() will use.
560 * @param context Keberos 5 context.
561 * @param in in ctx to krb5_rd_req_ctx().
562 * @param keytab keytab that krb5_rd_req_ctx() will use, only copy the
563 * pointer, so the caller must free they keytab after
564 * krb5_rd_req_in_ctx_free() is called.
566 * @return Kerberos 5 error code, see krb5_get_error_message().
571 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
572 krb5_rd_req_in_set_keytab(krb5_context context,
573 krb5_rd_req_in_ctx in,
581 * Set if krb5_rq_red() is going to check the Windows PAC or not
583 * @param context Keberos 5 context.
584 * @param in krb5_rd_req_in_ctx to check the option on.
585 * @param flag flag to select if to check the pac (TRUE) or not (FALSE).
587 * @return Kerberos 5 error code, see krb5_get_error_message().
592 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
593 krb5_rd_req_in_set_pac_check(krb5_context context,
594 krb5_rd_req_in_ctx in,
597 in->check_pac = flag;
602 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
603 krb5_rd_req_in_set_keyblock(krb5_context context,
604 krb5_rd_req_in_ctx in,
605 krb5_keyblock *keyblock)
607 in->keyblock = keyblock; /* XXX should make copy */
611 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
612 krb5_rd_req_out_get_ap_req_options(krb5_context context,
613 krb5_rd_req_out_ctx out,
614 krb5_flags *ap_req_options)
616 *ap_req_options = out->ap_req_options;
620 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
621 krb5_rd_req_out_get_ticket(krb5_context context,
622 krb5_rd_req_out_ctx out,
623 krb5_ticket **ticket)
625 return krb5_copy_ticket(context, out->ticket, ticket);
628 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
629 krb5_rd_req_out_get_keyblock(krb5_context context,
630 krb5_rd_req_out_ctx out,
631 krb5_keyblock **keyblock)
633 return krb5_copy_keyblock(context, out->keyblock, keyblock);
637 * Get the principal that was used in the request from the
638 * client. Might not match whats in the ticket if krb5_rd_req_ctx()
639 * searched in the keytab for a matching key.
641 * @param context a Kerberos 5 context.
642 * @param out a krb5_rd_req_out_ctx from krb5_rd_req_ctx().
643 * @param principal return principal, free with krb5_free_principal().
648 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
649 krb5_rd_req_out_get_server(krb5_context context,
650 krb5_rd_req_out_ctx out,
651 krb5_principal *principal)
653 return krb5_copy_principal(context, out->server, principal);
656 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
657 krb5_rd_req_in_ctx_free(krb5_context context, krb5_rd_req_in_ctx ctx)
663 * Free the krb5_rd_req_out_ctx.
665 * @param context Keberos 5 context.
666 * @param ctx krb5_rd_req_out_ctx context to free.
671 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
672 krb5_rd_req_out_ctx_free(krb5_context context, krb5_rd_req_out_ctx ctx)
675 krb5_free_ticket(context, ctx->ticket);
677 krb5_free_keyblock(context, ctx->keyblock);
679 krb5_free_principal(context, ctx->server);
687 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
688 krb5_rd_req(krb5_context context,
689 krb5_auth_context *auth_context,
690 const krb5_data *inbuf,
691 krb5_const_principal server,
693 krb5_flags *ap_req_options,
694 krb5_ticket **ticket)
697 krb5_rd_req_in_ctx in;
698 krb5_rd_req_out_ctx out;
700 ret = krb5_rd_req_in_ctx_alloc(context, &in);
704 ret = krb5_rd_req_in_set_keytab(context, in, keytab);
706 krb5_rd_req_in_ctx_free(context, in);
710 ret = krb5_rd_req_ctx(context, auth_context, inbuf, server, in, &out);
711 krb5_rd_req_in_ctx_free(context, in);
716 *ap_req_options = out->ap_req_options;
718 ret = krb5_copy_ticket(context, out->ticket, ticket);
724 krb5_rd_req_out_ctx_free(context, out);
732 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
733 krb5_rd_req_with_keyblock(krb5_context context,
734 krb5_auth_context *auth_context,
735 const krb5_data *inbuf,
736 krb5_const_principal server,
737 krb5_keyblock *keyblock,
738 krb5_flags *ap_req_options,
739 krb5_ticket **ticket)
742 krb5_rd_req_in_ctx in;
743 krb5_rd_req_out_ctx out;
745 ret = krb5_rd_req_in_ctx_alloc(context, &in);
749 ret = krb5_rd_req_in_set_keyblock(context, in, keyblock);
751 krb5_rd_req_in_ctx_free(context, in);
755 ret = krb5_rd_req_ctx(context, auth_context, inbuf, server, in, &out);
756 krb5_rd_req_in_ctx_free(context, in);
761 *ap_req_options = out->ap_req_options;
763 ret = krb5_copy_ticket(context, out->ticket, ticket);
769 krb5_rd_req_out_ctx_free(context, out);
777 static krb5_error_code
778 get_key_from_keytab(krb5_context context,
780 krb5_const_principal server,
782 krb5_keyblock **out_key)
784 krb5_keytab_entry entry;
787 krb5_keytab real_keytab;
790 krb5_kt_default(context, &real_keytab);
792 real_keytab = keytab;
794 if (ap_req->ticket.enc_part.kvno)
795 kvno = *ap_req->ticket.enc_part.kvno;
799 ret = krb5_kt_get_entry (context,
803 ap_req->ticket.enc_part.etype,
807 ret = krb5_copy_keyblock(context, &entry.keyblock, out_key);
808 krb5_kt_free_entry (context, &entry);
811 krb5_kt_close(context, real_keytab);
817 * The core server function that verify application authentication
818 * requests from clients.
820 * @param context Keberos 5 context.
821 * @param auth_context the authentication context, can be NULL, then
822 * default values for the authentication context will used.
823 * @param inbuf the (AP-REQ) authentication buffer
825 * @param server the server with authenticate as, if NULL the function
826 * will try to find any available credential in the keytab
827 * that will verify the reply. The function will prefer the
828 * server the server client specified in the AP-REQ, but if
829 * there is no mach, it will try all keytab entries for a
830 * match. This have serious performance issues for larger keytabs.
832 * @param inctx control the behavior of the function, if NULL, the
833 * default behavior is used.
834 * @param outctx the return outctx, free with krb5_rd_req_out_ctx_free().
835 * @return Kerberos 5 error code, see krb5_get_error_message().
840 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
841 krb5_rd_req_ctx(krb5_context context,
842 krb5_auth_context *auth_context,
843 const krb5_data *inbuf,
844 krb5_const_principal server,
845 krb5_rd_req_in_ctx inctx,
846 krb5_rd_req_out_ctx *outctx)
850 krb5_rd_req_out_ctx o = NULL;
851 krb5_keytab id = NULL, keytab = NULL;
852 krb5_principal service = NULL;
856 o = calloc(1, sizeof(*o));
858 krb5_set_error_message(context, ENOMEM,
859 N_("malloc: out of memory", ""));
863 if (*auth_context == NULL) {
864 ret = krb5_auth_con_init(context, auth_context);
869 ret = krb5_decode_ap_req(context, inbuf, &ap_req);
873 /* Save that principal that was in the request */
874 ret = _krb5_principalname2krb5_principal(context,
877 ap_req.ticket.realm);
881 if (ap_req.ap_options.use_session_key &&
882 (*auth_context)->keyblock == NULL) {
883 ret = KRB5KRB_AP_ERR_NOKEY;
884 krb5_set_error_message(context, ret,
885 N_("krb5_rd_req: user to user auth "
886 "without session key given", ""));
890 if (inctx && inctx->keytab)
893 if((*auth_context)->keyblock){
894 ret = krb5_copy_keyblock(context,
895 (*auth_context)->keyblock,
899 } else if(inctx && inctx->keyblock){
900 ret = krb5_copy_keyblock(context,
908 krb5_kt_default(context, &keytab);
914 if (server == NULL) {
915 ret = _krb5_principalname2krb5_principal(context,
918 ap_req.ticket.realm);
924 ret = get_key_from_keytab(context,
930 /* If caller specified a server, fail. */
931 if (service == NULL && (context->flags & KRB5_CTX_F_RD_REQ_IGNORE) == 0)
933 /* Otherwise, fall back to iterating over the keytab. This
934 * have serious performace issues for larger keytab.
942 * We got an exact keymatch, use that.
945 ret = krb5_verify_ap_req2(context,
953 KRB5_KU_AP_REQ_AUTH);
960 * Interate over keytab to find a key that can decrypt the request.
963 krb5_keytab_entry entry;
964 krb5_kt_cursor cursor;
965 int done = 0, kvno = 0;
967 memset(&cursor, 0, sizeof(cursor));
969 if (ap_req.ticket.enc_part.kvno)
970 kvno = *ap_req.ticket.enc_part.kvno;
972 ret = krb5_kt_start_seq_get(context, id, &cursor);
980 ret = krb5_kt_next_entry(context, id, &entry, &cursor);
982 _krb5_kt_principal_not_found(context, ret, id, o->server,
983 ap_req.ticket.enc_part.etype,
988 if (entry.keyblock.keytype != ap_req.ticket.enc_part.etype) {
989 krb5_kt_free_entry (context, &entry);
993 ret = krb5_verify_ap_req2(context,
1001 KRB5_KU_AP_REQ_AUTH);
1003 krb5_kt_free_entry (context, &entry);
1008 * Found a match, save the keyblock for PAC processing,
1009 * and update the service principal in the ticket to match
1010 * whatever is in the keytab.
1013 ret = krb5_copy_keyblock(context,
1017 krb5_kt_free_entry (context, &entry);
1021 ret = krb5_copy_principal(context, entry.principal, &p);
1023 krb5_kt_free_entry (context, &entry);
1026 krb5_free_principal(context, o->ticket->server);
1027 o->ticket->server = p;
1029 krb5_kt_free_entry (context, &entry);
1033 krb5_kt_end_seq_get (context, id, &cursor);
1036 /* If there is a PAC, verify its server signature */
1037 if (inctx == NULL || inctx->check_pac) {
1041 ret = krb5_ticket_get_authorization_data_type(context,
1043 KRB5_AUTHDATA_WIN2K_PAC,
1046 ret = krb5_pac_parse(context, data.data, data.length, &pac);
1047 krb5_data_free(&data);
1051 ret = krb5_pac_verify(context,
1053 o->ticket->ticket.authtime,
1057 krb5_pac_free(context, pac);
1065 if (ret || outctx == NULL) {
1066 krb5_rd_req_out_ctx_free(context, o);
1070 free_AP_REQ(&ap_req);
1073 krb5_free_principal(context, service);
1076 krb5_kt_close(context, keytab);