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)
136 unsigned int num_realms;
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,
392 ret = decrypt_authenticator (context,
394 &ap_req->authenticator,
401 krb5_principal p1, p2;
404 _krb5_principalname2krb5_principal(context,
406 ac->authenticator->cname,
407 ac->authenticator->crealm);
408 _krb5_principalname2krb5_principal(context,
412 res = krb5_principal_compare (context, p1, p2);
413 krb5_free_principal (context, p1);
414 krb5_free_principal (context, p2);
416 ret = KRB5KRB_AP_ERR_BADMATCH;
417 krb5_clear_error_string (context);
422 /* check addresses */
425 && ac->remote_address
426 && !krb5_address_search (context,
429 ret = KRB5KRB_AP_ERR_BADADDR;
430 krb5_clear_error_string (context);
434 /* check timestamp in authenticator */
438 krb5_timeofday (context, &now);
440 if (abs(ac->authenticator->ctime - now) > context->max_skew) {
441 ret = KRB5KRB_AP_ERR_SKEW;
442 krb5_clear_error_string (context);
447 if (ac->authenticator->seq_number)
448 krb5_auth_con_setremoteseqnumber(context, ac,
449 *ac->authenticator->seq_number);
451 /* XXX - Xor sequence numbers */
453 if (ac->authenticator->subkey) {
454 ret = krb5_auth_con_setremotesubkey(context, ac,
455 ac->authenticator->subkey);
460 ret = find_etypelist(context, ac, &etypes);
464 ac->keytype = ETYPE_NULL;
469 for (i = 0; i < etypes.len; i++) {
470 if (krb5_enctype_valid(context, etypes.val[i]) == 0) {
471 ac->keytype = etypes.val[i];
478 ret = krb5_copy_keyblock(context, &t->ticket.key, &ac->keyblock);
481 if (ap_req_options) {
483 if (ac->keytype != ETYPE_NULL)
484 *ap_req_options |= AP_OPTS_USE_SUBKEY;
485 if (ap_req->ap_options.use_session_key)
486 *ap_req_options |= AP_OPTS_USE_SESSION_KEY;
487 if (ap_req->ap_options.mutual_required)
488 *ap_req_options |= AP_OPTS_MUTUAL_REQUIRED;
494 krb5_free_ticket (context, t);
496 if (*auth_context == NULL)
499 krb5_auth_con_free (context, ac);
500 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;
530 krb5_error_code KRB5_LIB_FUNCTION
531 krb5_rd_req_in_ctx_alloc(krb5_context context, krb5_rd_req_in_ctx *ctx)
533 *ctx = calloc(1, sizeof(**ctx));
535 krb5_set_error_message(context, ENOMEM, "out of memory");
538 (*ctx)->check_pac = (context->flags & KRB5_CTX_F_CHECK_PAC) ? 1 : 0;
542 krb5_error_code KRB5_LIB_FUNCTION
543 krb5_rd_req_in_set_keytab(krb5_context context,
544 krb5_rd_req_in_ctx in,
547 in->keytab = keytab; /* XXX should make copy */
552 * Set if krb5_rq_red() is going to check the Windows PAC or not
554 * @param context Keberos 5 context.
555 * @param in krb5_rd_req_in_ctx to check the option on.
556 * @param flag flag to select if to check the pac (TRUE) or not (FALSE).
558 * @return Kerberos 5 error code, see krb5_get_error_message().
563 krb5_error_code KRB5_LIB_FUNCTION
564 krb5_rd_req_in_set_pac_check(krb5_context context,
565 krb5_rd_req_in_ctx in,
568 in->check_pac = flag;
573 krb5_error_code KRB5_LIB_FUNCTION
574 krb5_rd_req_in_set_keyblock(krb5_context context,
575 krb5_rd_req_in_ctx in,
576 krb5_keyblock *keyblock)
578 in->keyblock = keyblock; /* XXX should make copy */
582 krb5_error_code KRB5_LIB_FUNCTION
583 krb5_rd_req_out_get_ap_req_options(krb5_context context,
584 krb5_rd_req_out_ctx out,
585 krb5_flags *ap_req_options)
587 *ap_req_options = out->ap_req_options;
591 krb5_error_code KRB5_LIB_FUNCTION
592 krb5_rd_req_out_get_ticket(krb5_context context,
593 krb5_rd_req_out_ctx out,
594 krb5_ticket **ticket)
596 return krb5_copy_ticket(context, out->ticket, ticket);
599 krb5_error_code KRB5_LIB_FUNCTION
600 krb5_rd_req_out_get_keyblock(krb5_context context,
601 krb5_rd_req_out_ctx out,
602 krb5_keyblock **keyblock)
604 return krb5_copy_keyblock(context, out->keyblock, keyblock);
607 void KRB5_LIB_FUNCTION
608 krb5_rd_req_in_ctx_free(krb5_context context, krb5_rd_req_in_ctx ctx)
613 krb5_error_code KRB5_LIB_FUNCTION
614 _krb5_rd_req_out_ctx_alloc(krb5_context context, krb5_rd_req_out_ctx *ctx)
616 *ctx = calloc(1, sizeof(**ctx));
618 krb5_set_error_message(context, ENOMEM, "out of memory");
624 void KRB5_LIB_FUNCTION
625 krb5_rd_req_out_ctx_free(krb5_context context, krb5_rd_req_out_ctx ctx)
627 krb5_free_keyblock(context, ctx->keyblock);
635 krb5_error_code KRB5_LIB_FUNCTION
636 krb5_rd_req(krb5_context context,
637 krb5_auth_context *auth_context,
638 const krb5_data *inbuf,
639 krb5_const_principal server,
641 krb5_flags *ap_req_options,
642 krb5_ticket **ticket)
645 krb5_rd_req_in_ctx in;
646 krb5_rd_req_out_ctx out;
648 ret = krb5_rd_req_in_ctx_alloc(context, &in);
652 ret = krb5_rd_req_in_set_keytab(context, in, keytab);
654 krb5_rd_req_in_ctx_free(context, in);
658 ret = krb5_rd_req_ctx(context, auth_context, inbuf, server, in, &out);
659 krb5_rd_req_in_ctx_free(context, in);
664 *ap_req_options = out->ap_req_options;
666 ret = krb5_copy_ticket(context, out->ticket, ticket);
672 krb5_rd_req_out_ctx_free(context, out);
680 krb5_error_code KRB5_LIB_FUNCTION
681 krb5_rd_req_with_keyblock(krb5_context context,
682 krb5_auth_context *auth_context,
683 const krb5_data *inbuf,
684 krb5_const_principal server,
685 krb5_keyblock *keyblock,
686 krb5_flags *ap_req_options,
687 krb5_ticket **ticket)
690 krb5_rd_req_in_ctx in;
691 krb5_rd_req_out_ctx out;
693 ret = krb5_rd_req_in_ctx_alloc(context, &in);
697 ret = krb5_rd_req_in_set_keyblock(context, in, keyblock);
699 krb5_rd_req_in_ctx_free(context, in);
703 ret = krb5_rd_req_ctx(context, auth_context, inbuf, server, in, &out);
704 krb5_rd_req_in_ctx_free(context, in);
709 *ap_req_options = out->ap_req_options;
711 ret = krb5_copy_ticket(context, out->ticket, ticket);
717 krb5_rd_req_out_ctx_free(context, out);
725 static krb5_error_code
726 get_key_from_keytab(krb5_context context,
727 krb5_auth_context *auth_context,
729 krb5_const_principal server,
731 krb5_keyblock **out_key)
733 krb5_keytab_entry entry;
736 krb5_keytab real_keytab;
739 krb5_kt_default(context, &real_keytab);
741 real_keytab = keytab;
743 if (ap_req->ticket.enc_part.kvno)
744 kvno = *ap_req->ticket.enc_part.kvno;
748 ret = krb5_kt_get_entry (context,
752 ap_req->ticket.enc_part.etype,
756 ret = krb5_copy_keyblock(context, &entry.keyblock, out_key);
757 krb5_kt_free_entry (context, &entry);
760 krb5_kt_close(context, real_keytab);
769 krb5_error_code KRB5_LIB_FUNCTION
770 krb5_rd_req_ctx(krb5_context context,
771 krb5_auth_context *auth_context,
772 const krb5_data *inbuf,
773 krb5_const_principal server,
774 krb5_rd_req_in_ctx inctx,
775 krb5_rd_req_out_ctx *outctx)
779 krb5_principal service = NULL;
780 krb5_rd_req_out_ctx o = NULL;
782 ret = _krb5_rd_req_out_ctx_alloc(context, &o);
786 if (*auth_context == NULL) {
787 ret = krb5_auth_con_init(context, auth_context);
792 ret = krb5_decode_ap_req(context, inbuf, &ap_req);
797 ret = _krb5_principalname2krb5_principal(context,
800 ap_req.ticket.realm);
805 if (ap_req.ap_options.use_session_key &&
806 (*auth_context)->keyblock == NULL) {
807 ret = KRB5KRB_AP_ERR_NOKEY;
808 krb5_set_error_message(context, ret, "krb5_rd_req: user to user auth "
809 "without session key given");
813 if((*auth_context)->keyblock){
814 ret = krb5_copy_keyblock(context,
815 (*auth_context)->keyblock,
819 } else if(inctx->keyblock){
820 ret = krb5_copy_keyblock(context,
826 krb5_keytab keytab = NULL;
828 if (inctx && inctx->keytab)
829 keytab = inctx->keytab;
831 ret = get_key_from_keytab(context,
841 ret = krb5_verify_ap_req2(context,
849 KRB5_KU_AP_REQ_AUTH);
854 /* If there is a PAC, verify its server signature */
855 if (inctx->check_pac) {
859 ret = krb5_ticket_get_authorization_data_type(context,
861 KRB5_AUTHDATA_WIN2K_PAC,
864 ret = krb5_pac_parse(context, data.data, data.length, &pac);
865 krb5_data_free(&data);
869 ret = krb5_pac_verify(context,
871 o->ticket->ticket.authtime,
875 krb5_pac_free(context, pac);
882 if (ret || outctx == NULL) {
883 krb5_rd_req_out_ctx_free(context, o);
887 free_AP_REQ(&ap_req);
889 krb5_free_principal(context, service);