hdb: read_master_key use free_master_key on error
[heimdal.git] / lib / krb5 / rd_req.c
blobb16c88265fc4b9036e51c61c198db1cd474a780d
2 /*
3 * Copyright (c) 1997 - 2007 Kungliga Tekniska Högskolan
4 * (Royal Institute of Technology, Stockholm, Sweden).
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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
32 * SUCH DAMAGE.
35 #include "krb5_locl.h"
37 static krb5_error_code
38 decrypt_tkt_enc_part (krb5_context context,
39 krb5_keyblock *key,
40 EncryptedData *enc_part,
41 EncTicketPart *decr_part)
43 krb5_error_code ret;
44 krb5_data plain;
45 size_t len;
46 krb5_crypto crypto;
48 ret = krb5_crypto_init(context, key, 0, &crypto);
49 if (ret)
50 return ret;
51 ret = krb5_decrypt_EncryptedData (context,
52 crypto,
53 KRB5_KU_TICKET,
54 enc_part,
55 &plain);
56 krb5_crypto_destroy(context, crypto);
57 if (ret)
58 return ret;
60 ret = decode_EncTicketPart(plain.data, plain.length, decr_part, &len);
61 if (ret)
62 krb5_set_error_message(context, ret,
63 N_("Failed to decode encrypted "
64 "ticket part", ""));
65 krb5_data_free (&plain);
66 return ret;
69 static krb5_error_code
70 decrypt_authenticator (krb5_context context,
71 EncryptionKey *key,
72 EncryptedData *enc_part,
73 Authenticator *authenticator,
74 krb5_key_usage usage)
76 krb5_error_code ret;
77 krb5_data plain;
78 size_t len;
79 krb5_crypto crypto;
81 ret = krb5_crypto_init(context, key, 0, &crypto);
82 if (ret)
83 return ret;
84 ret = krb5_decrypt_EncryptedData (context,
85 crypto,
86 usage /* KRB5_KU_AP_REQ_AUTH */,
87 enc_part,
88 &plain);
89 /* for backwards compatibility, also try the old usage */
90 if (ret && usage == KRB5_KU_TGS_REQ_AUTH)
91 ret = krb5_decrypt_EncryptedData (context,
92 crypto,
93 KRB5_KU_AP_REQ_AUTH,
94 enc_part,
95 &plain);
96 krb5_crypto_destroy(context, crypto);
97 if (ret)
98 return ret;
100 ret = decode_Authenticator(plain.data, plain.length,
101 authenticator, &len);
102 krb5_data_free (&plain);
103 return ret;
106 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
107 krb5_decode_ap_req(krb5_context context,
108 const krb5_data *inbuf,
109 krb5_ap_req *ap_req)
111 krb5_error_code ret;
112 size_t len;
113 ret = decode_AP_REQ(inbuf->data, inbuf->length, ap_req, &len);
114 if (ret)
115 return ret;
116 if (ap_req->pvno != 5){
117 free_AP_REQ(ap_req);
118 krb5_clear_error_message (context);
119 return KRB5KRB_AP_ERR_BADVERSION;
121 if (ap_req->msg_type != krb_ap_req){
122 free_AP_REQ(ap_req);
123 krb5_clear_error_message (context);
124 return KRB5KRB_AP_ERR_MSG_TYPE;
126 if (ap_req->ticket.tkt_vno != 5){
127 free_AP_REQ(ap_req);
128 krb5_clear_error_message (context);
129 return KRB5KRB_AP_ERR_BADVERSION;
131 return 0;
134 static krb5_error_code
135 check_transited(krb5_context context, Ticket *ticket, EncTicketPart *enc)
137 char **realms;
138 unsigned int num_realms, n;
139 krb5_error_code ret;
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)
147 return 0;
149 if(enc->transited.tr_type != DOMAIN_X500_COMPRESS)
150 return KRB5KDC_ERR_TRTYPE_NOSUPP;
152 if(enc->transited.contents.length == 0)
153 return 0;
155 ret = krb5_domain_x500_decode(context, enc->transited.contents,
156 &realms, &num_realms,
157 enc->crealm,
158 ticket->realm);
159 if(ret)
160 return ret;
161 ret = krb5_check_transited(context, enc->crealm,
162 ticket->realm,
163 realms, num_realms, NULL);
164 for (n = 0; n < num_realms; n++)
165 free(realms[n]);
166 free(realms);
167 return ret;
170 static krb5_error_code
171 find_etypelist(krb5_context context,
172 krb5_auth_context auth_context,
173 EtypeList *etypes)
175 krb5_error_code ret;
176 krb5_data data;
178 ret = _krb5_get_ad(context, auth_context->authenticator->authorization_data, NULL, KRB5_AUTHDATA_GSS_API_ETYPE_NEGOTIATION, &data);
179 if (ret)
180 return 0;
182 ret = decode_EtypeList(data.data, data.length, etypes, NULL);
183 krb5_data_free(&data);
184 if (ret)
185 krb5_clear_error_message(context);
187 return ret;
190 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
191 krb5_decrypt_ticket(krb5_context context,
192 Ticket *ticket,
193 krb5_keyblock *key,
194 EncTicketPart *out,
195 krb5_flags flags)
197 EncTicketPart t;
198 krb5_error_code ret;
199 ret = decrypt_tkt_enc_part (context, key, &ticket->enc_part, &t);
200 if (ret)
201 return ret;
204 krb5_timestamp now;
205 time_t start = t.authtime;
207 krb5_timeofday (context, &now);
208 if(t.starttime)
209 start = *t.starttime;
210 if(start - now > context->max_skew
211 || (t.flags.invalid
212 && !(flags & KRB5_VERIFY_AP_REQ_IGNORE_INVALID))) {
213 free_EncTicketPart(&t);
214 krb5_clear_error_message (context);
215 return KRB5KRB_AP_ERR_TKT_NYV;
217 if(now - t.endtime > context->max_skew) {
218 free_EncTicketPart(&t);
219 krb5_clear_error_message (context);
220 return KRB5KRB_AP_ERR_TKT_EXPIRED;
223 if(!t.flags.transited_policy_checked) {
224 ret = check_transited(context, ticket, &t);
225 if(ret) {
226 free_EncTicketPart(&t);
227 return ret;
232 if(out)
233 *out = t;
234 else
235 free_EncTicketPart(&t);
236 return 0;
239 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
240 krb5_verify_authenticator_checksum(krb5_context context,
241 krb5_auth_context ac,
242 void *data,
243 size_t len)
245 krb5_error_code ret;
246 krb5_keyblock *key;
247 krb5_authenticator authenticator;
248 krb5_crypto crypto;
250 ret = krb5_auth_con_getauthenticator (context,
252 &authenticator);
253 if(ret)
254 return ret;
255 if(authenticator->cksum == NULL) {
256 krb5_free_authenticator(context, &authenticator);
257 return -17;
259 ret = krb5_auth_con_getkey(context, ac, &key);
260 if(ret) {
261 krb5_free_authenticator(context, &authenticator);
262 return ret;
264 ret = krb5_crypto_init(context, key, 0, &crypto);
265 if(ret)
266 goto out;
267 ret = krb5_verify_checksum (context,
268 crypto,
269 KRB5_KU_AP_REQ_AUTH_CKSUM,
270 data,
271 len,
272 authenticator->cksum);
273 krb5_crypto_destroy(context, crypto);
274 out:
275 krb5_free_authenticator(context, &authenticator);
276 krb5_free_keyblock(context, key);
277 return ret;
281 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
282 krb5_verify_ap_req(krb5_context context,
283 krb5_auth_context *auth_context,
284 krb5_ap_req *ap_req,
285 krb5_const_principal server,
286 krb5_keyblock *keyblock,
287 krb5_flags flags,
288 krb5_flags *ap_req_options,
289 krb5_ticket **ticket)
291 return krb5_verify_ap_req2 (context,
292 auth_context,
293 ap_req,
294 server,
295 keyblock,
296 flags,
297 ap_req_options,
298 ticket,
299 KRB5_KU_AP_REQ_AUTH);
302 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
303 krb5_verify_ap_req2(krb5_context context,
304 krb5_auth_context *auth_context,
305 krb5_ap_req *ap_req,
306 krb5_const_principal server,
307 krb5_keyblock *keyblock,
308 krb5_flags flags,
309 krb5_flags *ap_req_options,
310 krb5_ticket **ticket,
311 krb5_key_usage usage)
313 krb5_ticket *t;
314 krb5_auth_context ac;
315 krb5_error_code ret;
316 EtypeList etypes;
318 memset(&etypes, 0, sizeof(etypes));
320 if (ticket)
321 *ticket = NULL;
323 if (auth_context && *auth_context) {
324 ac = *auth_context;
325 } else {
326 ret = krb5_auth_con_init (context, &ac);
327 if (ret)
328 return ret;
331 t = calloc(1, sizeof(*t));
332 if (t == NULL) {
333 ret = krb5_enomem(context);
334 goto out;
337 if (ap_req->ap_options.use_session_key && ac->keyblock){
338 ret = krb5_decrypt_ticket(context, &ap_req->ticket,
339 ac->keyblock,
340 &t->ticket,
341 flags);
342 krb5_free_keyblock(context, ac->keyblock);
343 ac->keyblock = NULL;
344 }else
345 ret = krb5_decrypt_ticket(context, &ap_req->ticket,
346 keyblock,
347 &t->ticket,
348 flags);
350 if(ret)
351 goto out;
353 ret = _krb5_principalname2krb5_principal(context,
354 &t->server,
355 ap_req->ticket.sname,
356 ap_req->ticket.realm);
357 if (ret) goto out;
358 ret = _krb5_principalname2krb5_principal(context,
359 &t->client,
360 t->ticket.cname,
361 t->ticket.crealm);
362 if (ret) goto out;
364 ret = decrypt_authenticator (context,
365 &t->ticket.key,
366 &ap_req->authenticator,
367 ac->authenticator,
368 usage);
369 if (ret)
370 goto out;
373 krb5_principal p1, p2;
374 krb5_boolean res;
376 _krb5_principalname2krb5_principal(context,
377 &p1,
378 ac->authenticator->cname,
379 ac->authenticator->crealm);
380 _krb5_principalname2krb5_principal(context,
381 &p2,
382 t->ticket.cname,
383 t->ticket.crealm);
384 res = krb5_principal_compare (context, p1, p2);
385 krb5_free_principal (context, p1);
386 krb5_free_principal (context, p2);
387 if (!res) {
388 ret = KRB5KRB_AP_ERR_BADMATCH;
389 krb5_clear_error_message (context);
390 goto out;
394 /* check addresses */
396 if (t->ticket.caddr
397 && ac->remote_address
398 && !krb5_address_search (context,
399 ac->remote_address,
400 t->ticket.caddr)) {
401 ret = KRB5KRB_AP_ERR_BADADDR;
402 krb5_clear_error_message (context);
403 goto out;
406 /* check timestamp in authenticator */
408 krb5_timestamp now;
410 krb5_timeofday (context, &now);
412 if (labs(ac->authenticator->ctime - now) > context->max_skew) {
413 ret = KRB5KRB_AP_ERR_SKEW;
414 krb5_clear_error_message (context);
415 goto out;
419 if (ac->authenticator->seq_number)
420 krb5_auth_con_setremoteseqnumber(context, ac,
421 *ac->authenticator->seq_number);
423 /* XXX - Xor sequence numbers */
425 if (ac->authenticator->subkey) {
426 ret = krb5_auth_con_setremotesubkey(context, ac,
427 ac->authenticator->subkey);
428 if (ret)
429 goto out;
432 ret = find_etypelist(context, ac, &etypes);
433 if (ret)
434 goto out;
436 ac->keytype = ETYPE_NULL;
438 if (etypes.val) {
439 size_t i;
441 for (i = 0; i < etypes.len; i++) {
442 if (krb5_enctype_valid(context, etypes.val[i]) == 0) {
443 ac->keytype = etypes.val[i];
444 break;
449 /* save key */
450 ret = krb5_copy_keyblock(context, &t->ticket.key, &ac->keyblock);
451 if (ret) goto out;
453 if (ap_req_options) {
454 *ap_req_options = 0;
455 if (ac->keytype != (krb5_enctype)ETYPE_NULL)
456 *ap_req_options |= AP_OPTS_USE_SUBKEY;
457 if (ap_req->ap_options.use_session_key)
458 *ap_req_options |= AP_OPTS_USE_SESSION_KEY;
459 if (ap_req->ap_options.mutual_required)
460 *ap_req_options |= AP_OPTS_MUTUAL_REQUIRED;
463 if(ticket)
464 *ticket = t;
465 else
466 krb5_free_ticket (context, t);
467 if (auth_context) {
468 if (*auth_context == NULL)
469 *auth_context = ac;
470 } else
471 krb5_auth_con_free (context, ac);
472 free_EtypeList(&etypes);
473 return 0;
474 out:
475 free_EtypeList(&etypes);
476 if (t)
477 krb5_free_ticket (context, t);
478 if (auth_context == NULL || *auth_context == NULL)
479 krb5_auth_con_free (context, ac);
480 return ret;
487 struct krb5_rd_req_in_ctx_data {
488 krb5_keytab keytab;
489 krb5_keyblock *keyblock;
490 krb5_boolean check_pac;
493 struct krb5_rd_req_out_ctx_data {
494 krb5_keyblock *keyblock;
495 krb5_flags ap_req_options;
496 krb5_ticket *ticket;
497 krb5_principal server;
501 * Allocate a krb5_rd_req_in_ctx as an input parameter to
502 * krb5_rd_req_ctx(). The caller should free the context with
503 * krb5_rd_req_in_ctx_free() when done with the context.
505 * @param context Keberos 5 context.
506 * @param ctx in ctx to krb5_rd_req_ctx().
508 * @return Kerberos 5 error code, see krb5_get_error_message().
510 * @ingroup krb5_auth
513 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
514 krb5_rd_req_in_ctx_alloc(krb5_context context, krb5_rd_req_in_ctx *ctx)
516 *ctx = calloc(1, sizeof(**ctx));
517 if (*ctx == NULL)
518 return krb5_enomem(context);
519 (*ctx)->check_pac = (context->flags & KRB5_CTX_F_CHECK_PAC) ? 1 : 0;
520 return 0;
524 * Set the keytab that krb5_rd_req_ctx() will use.
526 * @param context Keberos 5 context.
527 * @param in in ctx to krb5_rd_req_ctx().
528 * @param keytab keytab that krb5_rd_req_ctx() will use, only copy the
529 * pointer, so the caller must free they keytab after
530 * krb5_rd_req_in_ctx_free() is called.
532 * @return Kerberos 5 error code, see krb5_get_error_message().
534 * @ingroup krb5_auth
537 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
538 krb5_rd_req_in_set_keytab(krb5_context context,
539 krb5_rd_req_in_ctx in,
540 krb5_keytab keytab)
542 in->keytab = keytab;
543 return 0;
547 * Set if krb5_rq_red() is going to check the Windows PAC or not
549 * @param context Keberos 5 context.
550 * @param in krb5_rd_req_in_ctx to check the option on.
551 * @param flag flag to select if to check the pac (TRUE) or not (FALSE).
553 * @return Kerberos 5 error code, see krb5_get_error_message().
555 * @ingroup krb5_auth
558 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
559 krb5_rd_req_in_set_pac_check(krb5_context context,
560 krb5_rd_req_in_ctx in,
561 krb5_boolean flag)
563 in->check_pac = flag;
564 return 0;
568 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
569 krb5_rd_req_in_set_keyblock(krb5_context context,
570 krb5_rd_req_in_ctx in,
571 krb5_keyblock *keyblock)
573 in->keyblock = keyblock; /* XXX should make copy */
574 return 0;
577 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
578 krb5_rd_req_out_get_ap_req_options(krb5_context context,
579 krb5_rd_req_out_ctx out,
580 krb5_flags *ap_req_options)
582 *ap_req_options = out->ap_req_options;
583 return 0;
586 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
587 krb5_rd_req_out_get_ticket(krb5_context context,
588 krb5_rd_req_out_ctx out,
589 krb5_ticket **ticket)
591 return krb5_copy_ticket(context, out->ticket, ticket);
594 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
595 krb5_rd_req_out_get_keyblock(krb5_context context,
596 krb5_rd_req_out_ctx out,
597 krb5_keyblock **keyblock)
599 return krb5_copy_keyblock(context, out->keyblock, keyblock);
603 * Get the principal that was used in the request from the
604 * client. Might not match whats in the ticket if krb5_rd_req_ctx()
605 * searched in the keytab for a matching key.
607 * @param context a Kerberos 5 context.
608 * @param out a krb5_rd_req_out_ctx from krb5_rd_req_ctx().
609 * @param principal return principal, free with krb5_free_principal().
611 * @ingroup krb5_auth
614 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
615 krb5_rd_req_out_get_server(krb5_context context,
616 krb5_rd_req_out_ctx out,
617 krb5_principal *principal)
619 return krb5_copy_principal(context, out->server, principal);
622 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
623 krb5_rd_req_in_ctx_free(krb5_context context, krb5_rd_req_in_ctx ctx)
625 free(ctx);
629 * Free the krb5_rd_req_out_ctx.
631 * @param context Keberos 5 context.
632 * @param ctx krb5_rd_req_out_ctx context to free.
634 * @ingroup krb5_auth
637 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
638 krb5_rd_req_out_ctx_free(krb5_context context, krb5_rd_req_out_ctx ctx)
640 if (ctx->ticket)
641 krb5_free_ticket(context, ctx->ticket);
642 if (ctx->keyblock)
643 krb5_free_keyblock(context, ctx->keyblock);
644 if (ctx->server)
645 krb5_free_principal(context, ctx->server);
646 free(ctx);
650 * Process an AP_REQ message.
652 * @param context Kerberos 5 context.
653 * @param auth_context authentication context of the peer.
654 * @param inbuf the AP_REQ message, obtained for example with krb5_read_message().
655 * @param server server principal.
656 * @param keytab server keytab.
657 * @param ap_req_options set to the AP_REQ options. See the AP_OPTS_* defines.
658 * @param ticket on success, set to the authenticated client credentials.
659 * Must be deallocated with krb5_free_ticket(). If not
660 * interested, pass a NULL value.
662 * @return 0 to indicate success. Otherwise a Kerberos error code is
663 * returned, see krb5_get_error_message().
665 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
666 krb5_rd_req(krb5_context context,
667 krb5_auth_context *auth_context,
668 const krb5_data *inbuf,
669 krb5_const_principal server,
670 krb5_keytab keytab,
671 krb5_flags *ap_req_options,
672 krb5_ticket **ticket)
674 krb5_error_code ret;
675 krb5_rd_req_in_ctx in;
676 krb5_rd_req_out_ctx out;
678 ret = krb5_rd_req_in_ctx_alloc(context, &in);
679 if (ret)
680 return ret;
682 ret = krb5_rd_req_in_set_keytab(context, in, keytab);
683 if (ret) {
684 krb5_rd_req_in_ctx_free(context, in);
685 return ret;
688 ret = krb5_rd_req_ctx(context, auth_context, inbuf, server, in, &out);
689 krb5_rd_req_in_ctx_free(context, in);
690 if (ret)
691 return ret;
693 if (ap_req_options)
694 *ap_req_options = out->ap_req_options;
695 if (ticket) {
696 ret = krb5_copy_ticket(context, out->ticket, ticket);
697 if (ret)
698 goto out;
701 out:
702 krb5_rd_req_out_ctx_free(context, out);
703 return ret;
710 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
711 krb5_rd_req_with_keyblock(krb5_context context,
712 krb5_auth_context *auth_context,
713 const krb5_data *inbuf,
714 krb5_const_principal server,
715 krb5_keyblock *keyblock,
716 krb5_flags *ap_req_options,
717 krb5_ticket **ticket)
719 krb5_error_code ret;
720 krb5_rd_req_in_ctx in;
721 krb5_rd_req_out_ctx out;
723 ret = krb5_rd_req_in_ctx_alloc(context, &in);
724 if (ret)
725 return ret;
727 ret = krb5_rd_req_in_set_keyblock(context, in, keyblock);
728 if (ret) {
729 krb5_rd_req_in_ctx_free(context, in);
730 return ret;
733 ret = krb5_rd_req_ctx(context, auth_context, inbuf, server, in, &out);
734 krb5_rd_req_in_ctx_free(context, in);
735 if (ret)
736 return ret;
738 if (ap_req_options)
739 *ap_req_options = out->ap_req_options;
740 if (ticket) {
741 ret = krb5_copy_ticket(context, out->ticket, ticket);
742 if (ret)
743 goto out;
746 out:
747 krb5_rd_req_out_ctx_free(context, out);
748 return ret;
755 static krb5_error_code
756 get_key_from_keytab(krb5_context context,
757 krb5_ap_req *ap_req,
758 krb5_const_principal server,
759 krb5_keytab keytab,
760 krb5_keyblock **out_key)
762 krb5_keytab_entry entry;
763 krb5_error_code ret;
764 int kvno;
765 krb5_keytab real_keytab;
767 if(keytab == NULL)
768 krb5_kt_default(context, &real_keytab);
769 else
770 real_keytab = keytab;
772 if (ap_req->ticket.enc_part.kvno)
773 kvno = *ap_req->ticket.enc_part.kvno;
774 else
775 kvno = 0;
777 ret = krb5_kt_get_entry (context,
778 real_keytab,
779 server,
780 kvno,
781 ap_req->ticket.enc_part.etype,
782 &entry);
783 if(ret)
784 goto out;
785 ret = krb5_copy_keyblock(context, &entry.keyblock, out_key);
786 krb5_kt_free_entry (context, &entry);
787 out:
788 if(keytab == NULL)
789 krb5_kt_close(context, real_keytab);
791 return ret;
795 * The core server function that verify application authentication
796 * requests from clients.
798 * @param context Keberos 5 context.
799 * @param auth_context the authentication context, can be NULL, then
800 * default values for the authentication context will used.
801 * @param inbuf the (AP-REQ) authentication buffer
803 * @param server the server to authenticate to. If NULL the function
804 * will try to find any available credential in the keytab
805 * that will verify the reply. The function will prefer the
806 * server specified in the AP-REQ, but if
807 * there is no mach, it will try all keytab entries for a
808 * match. This has serious performance issues for large keytabs.
810 * @param inctx control the behavior of the function, if NULL, the
811 * default behavior is used.
812 * @param outctx the return outctx, free with krb5_rd_req_out_ctx_free().
813 * @return Kerberos 5 error code, see krb5_get_error_message().
815 * @ingroup krb5_auth
818 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
819 krb5_rd_req_ctx(krb5_context context,
820 krb5_auth_context *auth_context,
821 const krb5_data *inbuf,
822 krb5_const_principal server,
823 krb5_rd_req_in_ctx inctx,
824 krb5_rd_req_out_ctx *outctx)
826 krb5_error_code ret;
827 krb5_ap_req ap_req;
828 krb5_rd_req_out_ctx o = NULL;
829 krb5_keytab id = NULL, keytab = NULL;
830 krb5_principal service = NULL;
832 *outctx = NULL;
834 o = calloc(1, sizeof(*o));
835 if (o == NULL)
836 return krb5_enomem(context);
838 if (*auth_context == NULL) {
839 ret = krb5_auth_con_init(context, auth_context);
840 if (ret)
841 goto out;
844 ret = krb5_decode_ap_req(context, inbuf, &ap_req);
845 if(ret)
846 goto out;
848 /* Save the principal that was in the request */
849 ret = _krb5_principalname2krb5_principal(context,
850 &o->server,
851 ap_req.ticket.sname,
852 ap_req.ticket.realm);
853 if (ret)
854 goto out;
856 if (ap_req.ap_options.use_session_key &&
857 (*auth_context)->keyblock == NULL) {
858 ret = KRB5KRB_AP_ERR_NOKEY;
859 krb5_set_error_message(context, ret,
860 N_("krb5_rd_req: user to user auth "
861 "without session key given", ""));
862 goto out;
865 if (inctx && inctx->keytab)
866 id = inctx->keytab;
868 if((*auth_context)->keyblock){
869 ret = krb5_copy_keyblock(context,
870 (*auth_context)->keyblock,
871 &o->keyblock);
872 if (ret)
873 goto out;
874 } else if(inctx && inctx->keyblock){
875 ret = krb5_copy_keyblock(context,
876 inctx->keyblock,
877 &o->keyblock);
878 if (ret)
879 goto out;
880 } else {
882 if(id == NULL) {
883 krb5_kt_default(context, &keytab);
884 id = keytab;
886 if (id == NULL)
887 goto out;
889 if (server == NULL) {
890 ret = _krb5_principalname2krb5_principal(context,
891 &service,
892 ap_req.ticket.sname,
893 ap_req.ticket.realm);
894 if (ret)
895 goto out;
896 server = service;
899 ret = get_key_from_keytab(context,
900 &ap_req,
901 server,
903 &o->keyblock);
904 if (ret) {
905 /* If caller specified a server, fail. */
906 if (service == NULL && (context->flags & KRB5_CTX_F_RD_REQ_IGNORE) == 0)
907 goto out;
908 /* Otherwise, fall back to iterating over the keytab. This
909 * have serious performace issues for larger keytab.
911 o->keyblock = NULL;
915 if (o->keyblock) {
917 * We got an exact keymatch, use that.
920 ret = krb5_verify_ap_req2(context,
921 auth_context,
922 &ap_req,
923 server,
924 o->keyblock,
926 &o->ap_req_options,
927 &o->ticket,
928 KRB5_KU_AP_REQ_AUTH);
930 if (ret)
931 goto out;
933 } else {
935 * Interate over keytab to find a key that can decrypt the request.
938 krb5_keytab_entry entry;
939 krb5_kt_cursor cursor;
940 int done = 0, kvno = 0;
942 memset(&cursor, 0, sizeof(cursor));
944 if (ap_req.ticket.enc_part.kvno)
945 kvno = *ap_req.ticket.enc_part.kvno;
947 ret = krb5_kt_start_seq_get(context, id, &cursor);
948 if (ret)
949 goto out;
951 done = 0;
952 while (!done) {
953 krb5_principal p;
955 ret = krb5_kt_next_entry(context, id, &entry, &cursor);
956 if (ret) {
957 _krb5_kt_principal_not_found(context, ret, id, o->server,
958 ap_req.ticket.enc_part.etype,
959 kvno);
960 break;
963 if (entry.keyblock.keytype != ap_req.ticket.enc_part.etype) {
964 krb5_kt_free_entry (context, &entry);
965 continue;
968 ret = krb5_verify_ap_req2(context,
969 auth_context,
970 &ap_req,
971 server,
972 &entry.keyblock,
974 &o->ap_req_options,
975 &o->ticket,
976 KRB5_KU_AP_REQ_AUTH);
977 if (ret) {
978 krb5_kt_free_entry (context, &entry);
979 continue;
983 * Found a match, save the keyblock for PAC processing,
984 * and update the service principal in the ticket to match
985 * whatever is in the keytab.
988 ret = krb5_copy_keyblock(context,
989 &entry.keyblock,
990 &o->keyblock);
991 if (ret) {
992 krb5_kt_free_entry (context, &entry);
993 break;
996 ret = krb5_copy_principal(context, entry.principal, &p);
997 if (ret) {
998 krb5_kt_free_entry (context, &entry);
999 break;
1001 krb5_free_principal(context, o->ticket->server);
1002 o->ticket->server = p;
1004 krb5_kt_free_entry (context, &entry);
1006 done = 1;
1008 krb5_kt_end_seq_get (context, id, &cursor);
1009 if (ret)
1010 goto out;
1013 /* If there is a PAC, verify its server signature */
1014 if (inctx == NULL || inctx->check_pac) {
1015 krb5_pac pac;
1016 krb5_data data;
1018 ret = krb5_ticket_get_authorization_data_type(context,
1019 o->ticket,
1020 KRB5_AUTHDATA_WIN2K_PAC,
1021 &data);
1022 if (ret == 0) {
1023 ret = krb5_pac_parse(context, data.data, data.length, &pac);
1024 krb5_data_free(&data);
1025 if (ret)
1026 goto out;
1028 ret = krb5_pac_verify(context,
1029 pac,
1030 o->ticket->ticket.authtime,
1031 o->ticket->client,
1032 o->keyblock,
1033 NULL);
1034 krb5_pac_free(context, pac);
1035 if (ret)
1036 goto out;
1037 } else
1038 ret = 0;
1040 out:
1042 if (ret || outctx == NULL) {
1043 krb5_rd_req_out_ctx_free(context, o);
1044 } else
1045 *outctx = o;
1047 free_AP_REQ(&ap_req);
1049 if (service)
1050 krb5_free_principal(context, service);
1052 if (keytab)
1053 krb5_kt_close(context, keytab);
1055 return ret;