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
, len
, authdata
, &len
, ret
);
98 ALLOC(req_body
->enc_authorization_data
, 1);
99 if (req_body
->enc_authorization_data
== NULL
) {
101 krb5_set_error_string(context
, "malloc: out of memory");
104 ret
= krb5_crypto_init(context
, key
, 0, &crypto
);
107 free (req_body
->enc_authorization_data
);
110 krb5_encrypt_EncryptedData(context
,
112 KRB5_KU_TGS_REQ_AUTH_DAT_SUBKEY
,
113 /* KRB5_KU_TGS_REQ_AUTH_DAT_SESSION? */
117 req_body
->enc_authorization_data
);
119 krb5_crypto_destroy(context
, crypto
);
121 req_body
->enc_authorization_data
= NULL
;
127 * Create a tgs-req in `t' with `addresses', `flags', `second_ticket'
128 * (if not-NULL), `in_creds', `krbtgt', and returning the generated
129 * subkey in `subkey'.
132 static krb5_error_code
133 init_tgs_req (krb5_context context
,
135 krb5_addresses
*addresses
,
136 krb5_kdc_flags flags
,
137 Ticket
*second_ticket
,
138 krb5_creds
*in_creds
,
141 krb5_keyblock
**subkey
,
143 krb5_key_usage usage
)
145 krb5_error_code ret
= 0;
147 memset(t
, 0, sizeof(*t
));
149 t
->msg_type
= krb_tgs_req
;
150 if (in_creds
->session
.keytype
) {
151 ALLOC_SEQ(&t
->req_body
.etype
, 1);
152 if(t
->req_body
.etype
.val
== NULL
) {
154 krb5_set_error_string(context
, "malloc: out of memory");
157 t
->req_body
.etype
.val
[0] = in_creds
->session
.keytype
;
159 ret
= krb5_init_etype(context
,
160 &t
->req_body
.etype
.len
,
161 &t
->req_body
.etype
.val
,
166 t
->req_body
.addresses
= addresses
;
167 t
->req_body
.kdc_options
= flags
.b
;
168 ret
= copy_Realm(&in_creds
->server
->realm
, &t
->req_body
.realm
);
171 ALLOC(t
->req_body
.sname
, 1);
172 if (t
->req_body
.sname
== NULL
) {
174 krb5_set_error_string(context
, "malloc: out of memory");
178 /* some versions of some code might require that the client be
179 present in TGS-REQs, but this is clearly against the spec */
181 ret
= copy_PrincipalName(&in_creds
->server
->name
, t
->req_body
.sname
);
185 /* req_body.till should be NULL if there is no endtime specified,
186 but old MIT code (like DCE secd) doesn't like that */
187 ALLOC(t
->req_body
.till
, 1);
188 if(t
->req_body
.till
== NULL
){
190 krb5_set_error_string(context
, "malloc: out of memory");
193 *t
->req_body
.till
= in_creds
->times
.endtime
;
195 t
->req_body
.nonce
= nonce
;
197 ALLOC(t
->req_body
.additional_tickets
, 1);
198 if (t
->req_body
.additional_tickets
== NULL
) {
200 krb5_set_error_string(context
, "malloc: out of memory");
203 ALLOC_SEQ(t
->req_body
.additional_tickets
, 1);
204 if (t
->req_body
.additional_tickets
->val
== NULL
) {
206 krb5_set_error_string(context
, "malloc: out of memory");
209 ret
= copy_Ticket(second_ticket
, t
->req_body
.additional_tickets
->val
);
214 if (t
->padata
== NULL
) {
216 krb5_set_error_string(context
, "malloc: out of memory");
219 ALLOC_SEQ(t
->padata
, 1);
220 if (t
->padata
->val
== NULL
) {
222 krb5_set_error_string(context
, "malloc: out of memory");
227 krb5_auth_context ac
;
228 krb5_keyblock
*key
= NULL
;
230 ret
= krb5_auth_con_init(context
, &ac
);
234 if (krb5_config_get_bool_default(context
, NULL
, FALSE
,
236 krbtgt
->server
->realm
,
237 "tgs_require_subkey",
240 ret
= krb5_generate_subkey (context
, &krbtgt
->session
, &key
);
242 krb5_auth_con_free (context
, ac
);
246 ret
= krb5_auth_con_setlocalsubkey(context
, ac
, key
);
249 krb5_free_keyblock (context
, key
);
250 krb5_auth_con_free (context
, ac
);
255 ret
= set_auth_data (context
, &t
->req_body
, &in_creds
->authdata
, key
);
258 krb5_free_keyblock (context
, key
);
259 krb5_auth_con_free (context
, ac
);
263 ret
= make_pa_tgs_req(context
,
271 krb5_free_keyblock (context
, key
);
272 krb5_auth_con_free(context
, ac
);
277 krb5_auth_con_free(context
, ac
);
281 t
->req_body
.addresses
= NULL
;
288 _krb5_get_krbtgt(krb5_context context
,
296 memset(&tmp_cred
, 0, sizeof(tmp_cred
));
298 ret
= krb5_cc_get_principal(context
, id
, &tmp_cred
.client
);
302 ret
= krb5_make_principal(context
,
309 krb5_free_principal(context
, tmp_cred
.client
);
312 ret
= krb5_get_credentials(context
,
317 krb5_free_principal(context
, tmp_cred
.client
);
318 krb5_free_principal(context
, tmp_cred
.server
);
324 /* DCE compatible decrypt proc */
325 static krb5_error_code
326 decrypt_tkt_with_subkey (krb5_context context
,
328 krb5_key_usage usage
,
329 krb5_const_pointer subkey
,
330 krb5_kdc_rep
*dec_rep
)
337 ret
= krb5_crypto_init(context
, key
, 0, &crypto
);
340 ret
= krb5_decrypt_EncryptedData (context
,
343 &dec_rep
->kdc_rep
.enc_part
,
345 krb5_crypto_destroy(context
, crypto
);
347 /* DCE compat -- try to decrypt with subkey */
348 ret
= krb5_crypto_init(context
, (krb5_keyblock
*)subkey
, 0, &crypto
);
351 ret
= krb5_decrypt_EncryptedData (context
,
353 KRB5_KU_TGS_REP_ENC_PART_SUB_KEY
,
354 &dec_rep
->kdc_rep
.enc_part
,
356 krb5_crypto_destroy(context
, crypto
);
361 ret
= krb5_decode_EncASRepPart(context
,
367 ret
= krb5_decode_EncTGSRepPart(context
,
372 krb5_data_free (&data
);
376 static krb5_error_code
377 get_cred_kdc_usage(krb5_context context
,
379 krb5_kdc_flags flags
,
380 krb5_addresses
*addresses
,
381 krb5_creds
*in_creds
,
383 krb5_creds
*out_creds
,
384 krb5_key_usage usage
)
393 krb5_keyblock
*subkey
= NULL
;
395 Ticket second_ticket
;
396 int send_to_kdc_flags
= 0;
398 krb5_data_zero(&resp
);
399 krb5_data_zero(&enc
);
401 krb5_generate_random_block(&nonce
, sizeof(nonce
));
404 if(flags
.b
.enc_tkt_in_skey
){
405 ret
= decode_Ticket(in_creds
->second_ticket
.data
,
406 in_creds
->second_ticket
.length
,
407 &second_ticket
, &len
);
412 ret
= init_tgs_req (context
,
416 flags
.b
.enc_tkt_in_skey
? &second_ticket
: NULL
,
423 if(flags
.b
.enc_tkt_in_skey
)
424 free_Ticket(&second_ticket
);
428 ASN1_MALLOC_ENCODE(TGS_REQ
, enc
.data
, enc
.length
, &req
, &len
, ret
);
431 if(enc
.length
!= len
)
432 krb5_abortx(context
, "internal error in ASN.1 encoder");
434 /* don't free addresses */
435 req
.req_body
.addresses
= NULL
;
442 ret
= krb5_sendto_kdc_flags (context
, &enc
,
443 &krbtgt
->server
->name
.name_string
.val
[1],
449 memset(&rep
, 0, sizeof(rep
));
450 if(decode_TGS_REP(resp
.data
, resp
.length
, &rep
.kdc_rep
, &len
) == 0){
451 ret
= krb5_copy_principal(context
,
456 ret
= krb5_copy_principal(context
,
461 /* this should go someplace else */
462 out_creds
->times
.endtime
= in_creds
->times
.endtime
;
464 ret
= _krb5_extract_ticket(context
,
469 KRB5_KU_TGS_REP_ENC_PART_SESSION
,
473 flags
.b
.request_anonymous
,
474 decrypt_tkt_with_subkey
,
476 krb5_free_kdc_rep(context
, &rep
);
477 } else if(krb5_rd_error(context
, &resp
, &error
) == 0) {
478 ret
= krb5_error_from_rd_error(context
, &error
, in_creds
);
479 krb5_free_error_contents(context
, &error
);
481 if (ret
== KRB5KRB_ERR_RESPONSE_TOO_BIG
&& !(send_to_kdc_flags
& KRB5_KRBHST_FLAGS_LARGE_MSG
)) {
482 send_to_kdc_flags
|= KRB5_KRBHST_FLAGS_LARGE_MSG
;
483 krb5_data_free(&resp
);
486 } else if(resp
.data
&& ((char*)resp
.data
)[0] == 4) {
487 ret
= KRB5KRB_AP_ERR_V4_REPLY
;
488 krb5_clear_error_string(context
);
490 ret
= KRB5KRB_AP_ERR_MSG_TYPE
;
491 krb5_clear_error_string(context
);
495 krb5_data_free(&resp
);
496 krb5_data_free(&enc
);
498 krb5_free_keyblock_contents(context
, subkey
);
505 static krb5_error_code
506 get_cred_kdc(krb5_context context
,
508 krb5_kdc_flags flags
,
509 krb5_addresses
*addresses
,
510 krb5_creds
*in_creds
,
512 krb5_creds
*out_creds
)
516 ret
= get_cred_kdc_usage(context
, id
, flags
, addresses
, in_creds
,
517 krbtgt
, out_creds
, KRB5_KU_TGS_REQ_AUTH
);
518 if (ret
== KRB5KRB_AP_ERR_BAD_INTEGRITY
) {
519 krb5_clear_error_string (context
);
520 ret
= get_cred_kdc_usage(context
, id
, flags
, addresses
, in_creds
,
521 krbtgt
, out_creds
, KRB5_KU_AP_REQ_AUTH
);
526 /* same as above, just get local addresses first */
528 static krb5_error_code
529 get_cred_kdc_la(krb5_context context
, krb5_ccache id
, krb5_kdc_flags flags
,
530 krb5_creds
*in_creds
, krb5_creds
*krbtgt
,
531 krb5_creds
*out_creds
)
534 krb5_addresses addresses
, *addrs
= &addresses
;
536 krb5_get_all_client_addrs(context
, &addresses
);
537 /* XXX this sucks. */
538 if(addresses
.len
== 0)
540 ret
= get_cred_kdc(context
, id
, flags
, addrs
,
541 in_creds
, krbtgt
, out_creds
);
542 krb5_free_addresses(context
, &addresses
);
547 krb5_get_kdc_cred(krb5_context context
,
549 krb5_kdc_flags flags
,
550 krb5_addresses
*addresses
,
551 Ticket
*second_ticket
,
552 krb5_creds
*in_creds
,
553 krb5_creds
**out_creds
559 *out_creds
= calloc(1, sizeof(**out_creds
));
560 if(*out_creds
== NULL
) {
561 krb5_set_error_string(context
, "malloc: out of memory");
564 ret
= _krb5_get_krbtgt (context
,
566 in_creds
->server
->realm
,
572 ret
= get_cred_kdc(context
, id
, flags
, addresses
,
573 in_creds
, krbtgt
, *out_creds
);
574 krb5_free_creds (context
, krbtgt
);
581 static krb5_error_code
582 find_cred(krb5_context context
,
584 krb5_principal server
,
586 krb5_creds
*out_creds
)
590 mcreds
.server
= server
;
591 ret
= krb5_cc_retrieve_cred(context
, id
, KRB5_TC_DONT_MATCH_REALM
,
595 while(tgts
&& *tgts
){
596 if(krb5_compare_creds(context
, KRB5_TC_DONT_MATCH_REALM
,
598 ret
= krb5_copy_creds_contents(context
, *tgts
, out_creds
);
603 krb5_clear_error_string(context
);
604 return KRB5_CC_NOTFOUND
;
607 static krb5_error_code
608 add_cred(krb5_context context
, krb5_creds
***tgts
, krb5_creds
*tkt
)
612 krb5_creds
**tmp
= *tgts
;
614 for(i
= 0; tmp
&& tmp
[i
]; i
++); /* XXX */
615 tmp
= realloc(tmp
, (i
+2)*sizeof(*tmp
));
617 krb5_set_error_string(context
, "malloc: out of memory");
621 ret
= krb5_copy_creds(context
, tkt
, &tmp
[i
]);
628 creds = cc_get_cred(server)
629 if(creds) return creds
630 tgt = cc_get_cred(krbtgt/server_realm@any_realm)
632 return get_cred_tgt(server, tgt)
633 if(client_realm == server_realm)
635 tgt = get_cred(krbtgt/server_realm@client_realm)
636 while(tgt_inst != server_realm)
637 tgt = get_cred(krbtgt/server_realm@tgt_inst)
638 return get_cred_tgt(server, tgt)
641 static krb5_error_code
642 get_cred_from_kdc_flags(krb5_context context
,
643 krb5_kdc_flags flags
,
645 krb5_creds
*in_creds
,
646 krb5_creds
**out_creds
,
647 krb5_creds
***ret_tgts
)
650 krb5_creds
*tgt
, tmp_creds
;
651 krb5_const_realm client_realm
, server_realm
, try_realm
;
655 client_realm
= *krb5_princ_realm(context
, in_creds
->client
);
656 server_realm
= *krb5_princ_realm(context
, in_creds
->server
);
657 memset(&tmp_creds
, 0, sizeof(tmp_creds
));
658 ret
= krb5_copy_principal(context
, in_creds
->client
, &tmp_creds
.client
);
662 try_realm
= krb5_config_get_string(context
, NULL
, "capaths",
663 client_realm
, server_realm
, NULL
);
666 /* XXX remove in future release */
667 if(try_realm
== NULL
)
668 try_realm
= krb5_config_get_string(context
, NULL
, "libdefaults",
669 "capath", server_realm
, NULL
);
672 if (try_realm
== NULL
)
673 try_realm
= client_realm
;
675 ret
= krb5_make_principal(context
,
682 krb5_free_principal(context
, tmp_creds
.client
);
687 /* XXX try krb5_cc_retrieve_cred first? */
688 ret
= find_cred(context
, ccache
, tmp_creds
.server
,
691 *out_creds
= calloc(1, sizeof(**out_creds
));
692 if(*out_creds
== NULL
) {
693 krb5_set_error_string(context
, "malloc: out of memory");
698 krb5_appdefault_boolean(context
, NULL
, tgts
.server
->realm
,
699 "no-addresses", FALSE
, &noaddr
);
702 ret
= get_cred_kdc(context
, ccache
, flags
, NULL
,
703 in_creds
, &tgts
, *out_creds
);
705 ret
= get_cred_kdc_la(context
, ccache
, flags
,
706 in_creds
, &tgts
, *out_creds
);
712 krb5_free_creds_contents(context
, &tgts
);
713 krb5_free_principal(context
, tmp_creds
.server
);
714 krb5_free_principal(context
, tmp_creds
.client
);
718 if(krb5_realm_compare(context
, in_creds
->client
, in_creds
->server
)) {
719 krb5_clear_error_string (context
);
720 return KRB5_CC_NOTFOUND
;
722 /* XXX this can loop forever */
724 general_string tgt_inst
;
726 ret
= get_cred_from_kdc_flags(context
, flags
, ccache
, &tmp_creds
,
729 krb5_free_principal(context
, tmp_creds
.server
);
730 krb5_free_principal(context
, tmp_creds
.client
);
733 ret
= add_cred(context
, ret_tgts
, tgt
);
735 krb5_free_principal(context
, tmp_creds
.server
);
736 krb5_free_principal(context
, tmp_creds
.client
);
739 tgt_inst
= tgt
->server
->name
.name_string
.val
[1];
740 if(strcmp(tgt_inst
, server_realm
) == 0)
742 krb5_free_principal(context
, tmp_creds
.server
);
743 ret
= krb5_make_principal(context
, &tmp_creds
.server
,
744 tgt_inst
, KRB5_TGS_NAME
, server_realm
, NULL
);
746 krb5_free_principal(context
, tmp_creds
.server
);
747 krb5_free_principal(context
, tmp_creds
.client
);
750 ret
= krb5_free_creds(context
, tgt
);
752 krb5_free_principal(context
, tmp_creds
.server
);
753 krb5_free_principal(context
, tmp_creds
.client
);
758 krb5_free_principal(context
, tmp_creds
.server
);
759 krb5_free_principal(context
, tmp_creds
.client
);
760 *out_creds
= calloc(1, sizeof(**out_creds
));
761 if(*out_creds
== NULL
) {
762 krb5_set_error_string(context
, "malloc: out of memory");
767 krb5_appdefault_boolean(context
, NULL
, tgt
->server
->realm
,
768 "no-addresses", FALSE
, &noaddr
);
770 ret
= get_cred_kdc (context
, ccache
, flags
, NULL
,
771 in_creds
, tgt
, *out_creds
);
773 ret
= get_cred_kdc_la(context
, ccache
, flags
,
774 in_creds
, tgt
, *out_creds
);
780 krb5_free_creds(context
, tgt
);
785 krb5_get_cred_from_kdc_opt(krb5_context context
,
787 krb5_creds
*in_creds
,
788 krb5_creds
**out_creds
,
789 krb5_creds
***ret_tgts
,
794 return get_cred_from_kdc_flags(context
, f
, ccache
,
795 in_creds
, out_creds
, ret_tgts
);
799 krb5_get_cred_from_kdc(krb5_context context
,
801 krb5_creds
*in_creds
,
802 krb5_creds
**out_creds
,
803 krb5_creds
***ret_tgts
)
805 return krb5_get_cred_from_kdc_opt(context
, ccache
,
806 in_creds
, out_creds
, ret_tgts
, 0);
811 krb5_get_credentials_with_flags(krb5_context context
,
813 krb5_kdc_flags flags
,
815 krb5_creds
*in_creds
,
816 krb5_creds
**out_creds
)
820 krb5_creds
*res_creds
;
824 res_creds
= calloc(1, sizeof(*res_creds
));
825 if (res_creds
== NULL
) {
826 krb5_set_error_string(context
, "malloc: out of memory");
830 ret
= krb5_cc_retrieve_cred(context
,
832 in_creds
->session
.keytype
?
833 KRB5_TC_MATCH_KEYTYPE
: 0,
834 in_creds
, res_creds
);
836 *out_creds
= res_creds
;
840 if(ret
!= KRB5_CC_END
)
842 if(options
& KRB5_GC_CACHED
) {
843 krb5_clear_error_string (context
);
844 return KRB5_CC_NOTFOUND
;
846 if(options
& KRB5_GC_USER_USER
)
847 flags
.b
.enc_tkt_in_skey
= 1;
849 ret
= get_cred_from_kdc_flags(context
, flags
, ccache
,
850 in_creds
, out_creds
, &tgts
);
851 for(i
= 0; tgts
&& tgts
[i
]; i
++) {
852 krb5_cc_store_cred(context
, ccache
, tgts
[i
]);
853 krb5_free_creds(context
, tgts
[i
]);
856 if(ret
== 0 && flags
.b
.enc_tkt_in_skey
== 0)
857 krb5_cc_store_cred(context
, ccache
, *out_creds
);
862 krb5_get_credentials(krb5_context context
,
865 krb5_creds
*in_creds
,
866 krb5_creds
**out_creds
)
868 krb5_kdc_flags flags
;
870 return krb5_get_credentials_with_flags(context
, options
, flags
,
871 ccache
, in_creds
, out_creds
);