2 * Copyright (c) 1997 - 2004 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>
39 * Take the `body' and encode it into `padata' using the credentials
43 static krb5_error_code
44 make_pa_tgs_req(krb5_context context
,
57 ASN1_MALLOC_ENCODE(KDC_REQ_BODY
, buf
, buf_size
, body
, &len
, ret
);
61 krb5_abortx(context
, "internal error in ASN.1 encoder");
65 ret
= _krb5_mk_req_internal(context
, &ac
, 0, &in_data
, creds
,
66 &padata
->padata_value
,
67 KRB5_KU_TGS_REQ_AUTH_CKSUM
,
69 /* KRB5_KU_TGS_REQ_AUTH */);
74 padata
->padata_type
= KRB5_PADATA_TGS_REQ
;
79 * Set the `enc-authorization-data' in `req_body' based on `authdata'
82 static krb5_error_code
83 set_auth_data (krb5_context context
,
84 KDC_REQ_BODY
*req_body
,
85 krb5_authdata
*authdata
,
94 ASN1_MALLOC_ENCODE(AuthorizationData
, buf
, buf_size
, authdata
,
99 krb5_abortx(context
, "internal error in ASN.1 encoder");
101 ALLOC(req_body
->enc_authorization_data
, 1);
102 if (req_body
->enc_authorization_data
== NULL
) {
104 krb5_set_error_string(context
, "malloc: out of memory");
107 ret
= krb5_crypto_init(context
, key
, 0, &crypto
);
110 free (req_body
->enc_authorization_data
);
111 req_body
->enc_authorization_data
= NULL
;
114 krb5_encrypt_EncryptedData(context
,
116 KRB5_KU_TGS_REQ_AUTH_DAT_SUBKEY
,
117 /* KRB5_KU_TGS_REQ_AUTH_DAT_SESSION? */
121 req_body
->enc_authorization_data
);
123 krb5_crypto_destroy(context
, crypto
);
125 req_body
->enc_authorization_data
= NULL
;
131 * Create a tgs-req in `t' with `addresses', `flags', `second_ticket'
132 * (if not-NULL), `in_creds', `krbtgt', and returning the generated
133 * subkey in `subkey'.
136 static krb5_error_code
137 init_tgs_req (krb5_context context
,
139 krb5_addresses
*addresses
,
140 krb5_kdc_flags flags
,
141 Ticket
*second_ticket
,
142 krb5_creds
*in_creds
,
145 krb5_keyblock
**subkey
,
147 krb5_key_usage usage
)
149 krb5_error_code ret
= 0;
151 memset(t
, 0, sizeof(*t
));
153 t
->msg_type
= krb_tgs_req
;
154 if (in_creds
->session
.keytype
) {
155 ALLOC_SEQ(&t
->req_body
.etype
, 1);
156 if(t
->req_body
.etype
.val
== NULL
) {
158 krb5_set_error_string(context
, "malloc: out of memory");
161 t
->req_body
.etype
.val
[0] = in_creds
->session
.keytype
;
163 ret
= krb5_init_etype(context
,
164 &t
->req_body
.etype
.len
,
165 &t
->req_body
.etype
.val
,
170 t
->req_body
.addresses
= addresses
;
171 t
->req_body
.kdc_options
= flags
.b
;
172 ret
= copy_Realm(&in_creds
->server
->realm
, &t
->req_body
.realm
);
175 ALLOC(t
->req_body
.sname
, 1);
176 if (t
->req_body
.sname
== NULL
) {
178 krb5_set_error_string(context
, "malloc: out of memory");
182 /* some versions of some code might require that the client be
183 present in TGS-REQs, but this is clearly against the spec */
185 ret
= copy_PrincipalName(&in_creds
->server
->name
, t
->req_body
.sname
);
189 /* req_body.till should be NULL if there is no endtime specified,
190 but old MIT code (like DCE secd) doesn't like that */
191 ALLOC(t
->req_body
.till
, 1);
192 if(t
->req_body
.till
== NULL
){
194 krb5_set_error_string(context
, "malloc: out of memory");
197 *t
->req_body
.till
= in_creds
->times
.endtime
;
199 t
->req_body
.nonce
= nonce
;
201 ALLOC(t
->req_body
.additional_tickets
, 1);
202 if (t
->req_body
.additional_tickets
== NULL
) {
204 krb5_set_error_string(context
, "malloc: out of memory");
207 ALLOC_SEQ(t
->req_body
.additional_tickets
, 1);
208 if (t
->req_body
.additional_tickets
->val
== NULL
) {
210 krb5_set_error_string(context
, "malloc: out of memory");
213 ret
= copy_Ticket(second_ticket
, t
->req_body
.additional_tickets
->val
);
218 if (t
->padata
== NULL
) {
220 krb5_set_error_string(context
, "malloc: out of memory");
223 ALLOC_SEQ(t
->padata
, 1);
224 if (t
->padata
->val
== NULL
) {
226 krb5_set_error_string(context
, "malloc: out of memory");
231 krb5_auth_context ac
;
232 krb5_keyblock
*key
= NULL
;
234 ret
= krb5_auth_con_init(context
, &ac
);
238 if (krb5_config_get_bool_default(context
, NULL
, FALSE
,
240 krbtgt
->server
->realm
,
241 "tgs_require_subkey",
244 ret
= krb5_generate_subkey (context
, &krbtgt
->session
, &key
);
246 krb5_auth_con_free (context
, ac
);
250 ret
= krb5_auth_con_setlocalsubkey(context
, ac
, key
);
253 krb5_free_keyblock (context
, key
);
254 krb5_auth_con_free (context
, ac
);
259 ret
= set_auth_data (context
, &t
->req_body
, &in_creds
->authdata
,
260 key
? key
: &krbtgt
->session
);
263 krb5_free_keyblock (context
, key
);
264 krb5_auth_con_free (context
, ac
);
268 ret
= make_pa_tgs_req(context
,
276 krb5_free_keyblock (context
, key
);
277 krb5_auth_con_free(context
, ac
);
282 krb5_auth_con_free(context
, ac
);
286 t
->req_body
.addresses
= NULL
;
293 _krb5_get_krbtgt(krb5_context context
,
301 memset(&tmp_cred
, 0, sizeof(tmp_cred
));
303 ret
= krb5_cc_get_principal(context
, id
, &tmp_cred
.client
);
307 ret
= krb5_make_principal(context
,
314 krb5_free_principal(context
, tmp_cred
.client
);
317 ret
= krb5_get_credentials(context
,
322 krb5_free_principal(context
, tmp_cred
.client
);
323 krb5_free_principal(context
, tmp_cred
.server
);
329 /* DCE compatible decrypt proc */
330 static krb5_error_code
331 decrypt_tkt_with_subkey (krb5_context context
,
333 krb5_key_usage usage
,
334 krb5_const_pointer subkey
,
335 krb5_kdc_rep
*dec_rep
)
342 ret
= krb5_crypto_init(context
, key
, 0, &crypto
);
345 ret
= krb5_decrypt_EncryptedData (context
,
348 &dec_rep
->kdc_rep
.enc_part
,
350 krb5_crypto_destroy(context
, crypto
);
352 /* DCE compat -- try to decrypt with subkey */
353 ret
= krb5_crypto_init(context
, (krb5_keyblock
*)subkey
, 0, &crypto
);
356 ret
= krb5_decrypt_EncryptedData (context
,
358 KRB5_KU_TGS_REP_ENC_PART_SUB_KEY
,
359 &dec_rep
->kdc_rep
.enc_part
,
361 krb5_crypto_destroy(context
, crypto
);
366 ret
= krb5_decode_EncASRepPart(context
,
372 ret
= krb5_decode_EncTGSRepPart(context
,
377 krb5_data_free (&data
);
381 static krb5_error_code
382 get_cred_kdc_usage(krb5_context context
,
384 krb5_kdc_flags flags
,
385 krb5_addresses
*addresses
,
386 krb5_creds
*in_creds
,
388 krb5_creds
*out_creds
,
389 krb5_key_usage usage
)
398 krb5_keyblock
*subkey
= NULL
;
400 Ticket second_ticket
;
401 int send_to_kdc_flags
= 0;
403 krb5_data_zero(&resp
);
404 krb5_data_zero(&enc
);
406 krb5_generate_random_block(&nonce
, sizeof(nonce
));
409 if(flags
.b
.enc_tkt_in_skey
){
410 ret
= decode_Ticket(in_creds
->second_ticket
.data
,
411 in_creds
->second_ticket
.length
,
412 &second_ticket
, &len
);
417 ret
= init_tgs_req (context
,
421 flags
.b
.enc_tkt_in_skey
? &second_ticket
: NULL
,
428 if(flags
.b
.enc_tkt_in_skey
)
429 free_Ticket(&second_ticket
);
433 ASN1_MALLOC_ENCODE(TGS_REQ
, enc
.data
, enc
.length
, &req
, &len
, ret
);
436 if(enc
.length
!= len
)
437 krb5_abortx(context
, "internal error in ASN.1 encoder");
439 /* don't free addresses */
440 req
.req_body
.addresses
= NULL
;
447 ret
= krb5_sendto_kdc_flags (context
, &enc
,
448 &krbtgt
->server
->name
.name_string
.val
[1],
454 memset(&rep
, 0, sizeof(rep
));
455 if(decode_TGS_REP(resp
.data
, resp
.length
, &rep
.kdc_rep
, &len
) == 0){
456 ret
= krb5_copy_principal(context
,
461 ret
= krb5_copy_principal(context
,
466 /* this should go someplace else */
467 out_creds
->times
.endtime
= in_creds
->times
.endtime
;
469 ret
= _krb5_extract_ticket(context
,
474 KRB5_KU_TGS_REP_ENC_PART_SESSION
,
478 flags
.b
.request_anonymous
,
479 decrypt_tkt_with_subkey
,
481 krb5_free_kdc_rep(context
, &rep
);
482 } else if(krb5_rd_error(context
, &resp
, &error
) == 0) {
483 ret
= krb5_error_from_rd_error(context
, &error
, in_creds
);
484 krb5_free_error_contents(context
, &error
);
486 if (ret
== KRB5KRB_ERR_RESPONSE_TOO_BIG
&& !(send_to_kdc_flags
& KRB5_KRBHST_FLAGS_LARGE_MSG
)) {
487 send_to_kdc_flags
|= KRB5_KRBHST_FLAGS_LARGE_MSG
;
488 krb5_data_free(&resp
);
491 } else if(resp
.data
&& ((char*)resp
.data
)[0] == 4) {
492 ret
= KRB5KRB_AP_ERR_V4_REPLY
;
493 krb5_clear_error_string(context
);
495 ret
= KRB5KRB_AP_ERR_MSG_TYPE
;
496 krb5_clear_error_string(context
);
500 krb5_data_free(&resp
);
501 krb5_data_free(&enc
);
503 krb5_free_keyblock_contents(context
, subkey
);
510 static krb5_error_code
511 get_cred_kdc(krb5_context context
,
513 krb5_kdc_flags flags
,
514 krb5_addresses
*addresses
,
515 krb5_creds
*in_creds
,
517 krb5_creds
*out_creds
)
521 ret
= get_cred_kdc_usage(context
, id
, flags
, addresses
, in_creds
,
522 krbtgt
, out_creds
, KRB5_KU_TGS_REQ_AUTH
);
523 if (ret
== KRB5KRB_AP_ERR_BAD_INTEGRITY
) {
524 krb5_clear_error_string (context
);
525 ret
= get_cred_kdc_usage(context
, id
, flags
, addresses
, in_creds
,
526 krbtgt
, out_creds
, KRB5_KU_AP_REQ_AUTH
);
531 /* same as above, just get local addresses first */
533 static krb5_error_code
534 get_cred_kdc_la(krb5_context context
, krb5_ccache id
, krb5_kdc_flags flags
,
535 krb5_creds
*in_creds
, krb5_creds
*krbtgt
,
536 krb5_creds
*out_creds
)
539 krb5_addresses addresses
, *addrs
= &addresses
;
541 krb5_get_all_client_addrs(context
, &addresses
);
542 /* XXX this sucks. */
543 if(addresses
.len
== 0)
545 ret
= get_cred_kdc(context
, id
, flags
, addrs
,
546 in_creds
, krbtgt
, out_creds
);
547 krb5_free_addresses(context
, &addresses
);
551 krb5_error_code KRB5_LIB_FUNCTION
552 krb5_get_kdc_cred(krb5_context context
,
554 krb5_kdc_flags flags
,
555 krb5_addresses
*addresses
,
556 Ticket
*second_ticket
,
557 krb5_creds
*in_creds
,
558 krb5_creds
**out_creds
564 *out_creds
= calloc(1, sizeof(**out_creds
));
565 if(*out_creds
== NULL
) {
566 krb5_set_error_string(context
, "malloc: out of memory");
569 ret
= _krb5_get_krbtgt (context
,
571 in_creds
->server
->realm
,
577 ret
= get_cred_kdc(context
, id
, flags
, addresses
,
578 in_creds
, krbtgt
, *out_creds
);
579 krb5_free_creds (context
, krbtgt
);
586 static krb5_error_code
587 find_cred(krb5_context context
,
589 krb5_principal server
,
591 krb5_creds
*out_creds
)
596 krb5_cc_clear_mcred(&mcreds
);
597 mcreds
.server
= server
;
598 ret
= krb5_cc_retrieve_cred(context
, id
, KRB5_TC_DONT_MATCH_REALM
,
602 while(tgts
&& *tgts
){
603 if(krb5_compare_creds(context
, KRB5_TC_DONT_MATCH_REALM
,
605 ret
= krb5_copy_creds_contents(context
, *tgts
, out_creds
);
610 krb5_clear_error_string(context
);
611 return KRB5_CC_NOTFOUND
;
614 static krb5_error_code
615 add_cred(krb5_context context
, krb5_creds
***tgts
, krb5_creds
*tkt
)
619 krb5_creds
**tmp
= *tgts
;
621 for(i
= 0; tmp
&& tmp
[i
]; i
++); /* XXX */
622 tmp
= realloc(tmp
, (i
+2)*sizeof(*tmp
));
624 krb5_set_error_string(context
, "malloc: out of memory");
628 ret
= krb5_copy_creds(context
, tkt
, &tmp
[i
]);
635 creds = cc_get_cred(server)
636 if(creds) return creds
637 tgt = cc_get_cred(krbtgt/server_realm@any_realm)
639 return get_cred_tgt(server, tgt)
640 if(client_realm == server_realm)
642 tgt = get_cred(krbtgt/server_realm@client_realm)
643 while(tgt_inst != server_realm)
644 tgt = get_cred(krbtgt/server_realm@tgt_inst)
645 return get_cred_tgt(server, tgt)
648 static krb5_error_code
649 get_cred_from_kdc_flags(krb5_context context
,
650 krb5_kdc_flags flags
,
652 krb5_creds
*in_creds
,
653 krb5_creds
**out_creds
,
654 krb5_creds
***ret_tgts
)
657 krb5_creds
*tgt
, tmp_creds
;
658 krb5_const_realm client_realm
, server_realm
, try_realm
;
662 client_realm
= krb5_principal_get_realm(context
, in_creds
->client
);
663 server_realm
= krb5_principal_get_realm(context
, in_creds
->server
);
664 memset(&tmp_creds
, 0, sizeof(tmp_creds
));
665 ret
= krb5_copy_principal(context
, in_creds
->client
, &tmp_creds
.client
);
669 try_realm
= krb5_config_get_string(context
, NULL
, "capaths",
670 client_realm
, server_realm
, NULL
);
673 /* XXX remove in future release */
674 if(try_realm
== NULL
)
675 try_realm
= krb5_config_get_string(context
, NULL
, "libdefaults",
676 "capath", server_realm
, NULL
);
679 if (try_realm
== NULL
)
680 try_realm
= client_realm
;
682 ret
= krb5_make_principal(context
,
689 krb5_free_principal(context
, tmp_creds
.client
);
694 /* XXX try krb5_cc_retrieve_cred first? */
695 ret
= find_cred(context
, ccache
, tmp_creds
.server
,
698 *out_creds
= calloc(1, sizeof(**out_creds
));
699 if(*out_creds
== NULL
) {
700 krb5_set_error_string(context
, "malloc: out of memory");
705 krb5_appdefault_boolean(context
, NULL
, tgts
.server
->realm
,
706 "no-addresses", FALSE
, &noaddr
);
709 ret
= get_cred_kdc(context
, ccache
, flags
, NULL
,
710 in_creds
, &tgts
, *out_creds
);
712 ret
= get_cred_kdc_la(context
, ccache
, flags
,
713 in_creds
, &tgts
, *out_creds
);
719 krb5_free_cred_contents(context
, &tgts
);
720 krb5_free_principal(context
, tmp_creds
.server
);
721 krb5_free_principal(context
, tmp_creds
.client
);
725 if(krb5_realm_compare(context
, in_creds
->client
, in_creds
->server
)) {
726 krb5_clear_error_string (context
);
727 return KRB5_CC_NOTFOUND
;
729 /* XXX this can loop forever */
731 heim_general_string tgt_inst
;
733 ret
= get_cred_from_kdc_flags(context
, flags
, ccache
, &tmp_creds
,
736 krb5_free_principal(context
, tmp_creds
.server
);
737 krb5_free_principal(context
, tmp_creds
.client
);
740 ret
= add_cred(context
, ret_tgts
, tgt
);
742 krb5_free_principal(context
, tmp_creds
.server
);
743 krb5_free_principal(context
, tmp_creds
.client
);
746 tgt_inst
= tgt
->server
->name
.name_string
.val
[1];
747 if(strcmp(tgt_inst
, server_realm
) == 0)
749 krb5_free_principal(context
, tmp_creds
.server
);
750 ret
= krb5_make_principal(context
, &tmp_creds
.server
,
751 tgt_inst
, KRB5_TGS_NAME
, server_realm
, NULL
);
753 krb5_free_principal(context
, tmp_creds
.server
);
754 krb5_free_principal(context
, tmp_creds
.client
);
757 ret
= krb5_free_creds(context
, tgt
);
759 krb5_free_principal(context
, tmp_creds
.server
);
760 krb5_free_principal(context
, tmp_creds
.client
);
765 krb5_free_principal(context
, tmp_creds
.server
);
766 krb5_free_principal(context
, tmp_creds
.client
);
767 *out_creds
= calloc(1, sizeof(**out_creds
));
768 if(*out_creds
== NULL
) {
769 krb5_set_error_string(context
, "malloc: out of memory");
774 krb5_appdefault_boolean(context
, NULL
, tgt
->server
->realm
,
775 "no-addresses", FALSE
, &noaddr
);
777 ret
= get_cred_kdc (context
, ccache
, flags
, NULL
,
778 in_creds
, tgt
, *out_creds
);
780 ret
= get_cred_kdc_la(context
, ccache
, flags
,
781 in_creds
, tgt
, *out_creds
);
787 krb5_free_creds(context
, tgt
);
791 krb5_error_code KRB5_LIB_FUNCTION
792 krb5_get_cred_from_kdc_opt(krb5_context context
,
794 krb5_creds
*in_creds
,
795 krb5_creds
**out_creds
,
796 krb5_creds
***ret_tgts
,
801 return get_cred_from_kdc_flags(context
, f
, ccache
,
802 in_creds
, out_creds
, ret_tgts
);
805 krb5_error_code KRB5_LIB_FUNCTION
806 krb5_get_cred_from_kdc(krb5_context context
,
808 krb5_creds
*in_creds
,
809 krb5_creds
**out_creds
,
810 krb5_creds
***ret_tgts
)
812 return krb5_get_cred_from_kdc_opt(context
, ccache
,
813 in_creds
, out_creds
, ret_tgts
, 0);
817 krb5_error_code KRB5_LIB_FUNCTION
818 krb5_get_credentials_with_flags(krb5_context context
,
820 krb5_kdc_flags flags
,
822 krb5_creds
*in_creds
,
823 krb5_creds
**out_creds
)
827 krb5_creds
*res_creds
;
831 res_creds
= calloc(1, sizeof(*res_creds
));
832 if (res_creds
== NULL
) {
833 krb5_set_error_string(context
, "malloc: out of memory");
837 if (in_creds
->session
.keytype
)
838 options
|= KRB5_TC_MATCH_KEYTYPE
;
841 * If we got a credential, check if credential is expired before
844 ret
= krb5_cc_retrieve_cred(context
,
846 in_creds
->session
.keytype
?
847 KRB5_TC_MATCH_KEYTYPE
: 0,
848 in_creds
, res_creds
);
850 * If we got a credential, check if credential is expired before
851 * returning it, but only if KRB5_GC_EXPIRED_OK is not set.
854 krb5_timestamp timeret
;
856 /* If expired ok, don't bother checking */
857 if(options
& KRB5_GC_EXPIRED_OK
) {
858 *out_creds
= res_creds
;
862 krb5_timeofday(context
, &timeret
);
863 if(res_creds
->times
.endtime
> timeret
) {
864 *out_creds
= res_creds
;
867 if(options
& KRB5_GC_CACHED
)
868 krb5_cc_remove_cred(context
, ccache
, 0, res_creds
);
870 } else if(ret
!= KRB5_CC_END
) {
875 if(options
& KRB5_GC_CACHED
) {
876 krb5_clear_error_string (context
);
877 return KRB5_CC_NOTFOUND
;
879 if(options
& KRB5_GC_USER_USER
)
880 flags
.b
.enc_tkt_in_skey
= 1;
882 ret
= get_cred_from_kdc_flags(context
, flags
, ccache
,
883 in_creds
, out_creds
, &tgts
);
884 for(i
= 0; tgts
&& tgts
[i
]; i
++) {
885 krb5_cc_store_cred(context
, ccache
, tgts
[i
]);
886 krb5_free_creds(context
, tgts
[i
]);
889 if(ret
== 0 && flags
.b
.enc_tkt_in_skey
== 0)
890 krb5_cc_store_cred(context
, ccache
, *out_creds
);
894 krb5_error_code KRB5_LIB_FUNCTION
895 krb5_get_credentials(krb5_context context
,
898 krb5_creds
*in_creds
,
899 krb5_creds
**out_creds
)
901 krb5_kdc_flags flags
;
903 return krb5_get_credentials_with_flags(context
, options
, flags
,
904 ccache
, in_creds
, out_creds
);