2 * Copyright (c) 1997 - 2002 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"
36 RCSID("$Id: get_in_tkt.c,v 1.107.2.1 2003/09/18 21:00:09 lha Exp $");
39 krb5_init_etype (krb5_context context,
42 const krb5_enctype *etypes)
46 krb5_enctype *tmp = NULL;
50 ret = krb5_get_default_in_tkt_etypes(context,
57 for (i = 0; etypes[i]; ++i)
60 *val = malloc(i * sizeof(**val));
61 if (i != 0 && *val == NULL) {
63 krb5_set_error_string(context, "malloc: out of memory");
76 static krb5_error_code
77 decrypt_tkt (krb5_context context,
80 krb5_const_pointer decrypt_arg,
81 krb5_kdc_rep *dec_rep)
88 ret = krb5_crypto_init(context, key, 0, &crypto);
92 ret = krb5_decrypt_EncryptedData (context,
95 &dec_rep->kdc_rep.enc_part,
97 krb5_crypto_destroy(context, crypto);
102 ret = krb5_decode_EncASRepPart(context,
108 ret = krb5_decode_EncTGSRepPart(context,
113 krb5_data_free (&data);
120 _krb5_extract_ticket(krb5_context context,
124 krb5_const_pointer keyseed,
125 krb5_key_usage key_usage,
126 krb5_addresses *addrs,
128 krb5_boolean allow_server_mismatch,
129 krb5_boolean ignore_cname,
130 krb5_decrypt_proc decrypt_proc,
131 krb5_const_pointer decryptarg)
134 krb5_principal tmp_principal;
137 krb5_timestamp sec_now;
139 ret = principalname2krb5_principal (&tmp_principal,
141 rep->kdc_rep.crealm);
148 tmp = krb5_principal_compare (context, tmp_principal, creds->client);
150 krb5_free_principal (context, tmp_principal);
151 krb5_clear_error_string (context);
152 ret = KRB5KRB_AP_ERR_MODIFIED;
157 krb5_free_principal (context, creds->client);
158 creds->client = tmp_principal;
161 ASN1_MALLOC_ENCODE(Ticket, creds->ticket.data, creds->ticket.length,
162 &rep->kdc_rep.ticket, &creds->ticket.length, ret);
165 creds->second_ticket.length = 0;
166 creds->second_ticket.data = NULL;
170 ret = principalname2krb5_principal (&tmp_principal,
171 rep->kdc_rep.ticket.sname,
172 rep->kdc_rep.ticket.realm);
175 if(allow_server_mismatch){
176 krb5_free_principal(context, creds->server);
177 creds->server = tmp_principal;
178 tmp_principal = NULL;
180 tmp = krb5_principal_compare (context, tmp_principal, creds->server);
181 krb5_free_principal (context, tmp_principal);
183 ret = KRB5KRB_AP_ERR_MODIFIED;
184 krb5_clear_error_string (context);
191 if (decrypt_proc == NULL)
192 decrypt_proc = decrypt_tkt;
194 ret = (*decrypt_proc)(context, key, key_usage, decryptarg, rep);
199 /* XXX should this decode be here, or in the decrypt_proc? */
200 ret = krb5_decode_keyblock(context, &rep->enc_part.key, 1);
207 if (nonce != rep->enc_part.nonce) {
208 ret = KRB5KRB_AP_ERR_MODIFIED;
209 krb5_set_error_string(context, "malloc: out of memory");
215 krb5_timeofday (context, &sec_now);
216 if (rep->enc_part.flags.initial
217 && context->kdc_sec_offset == 0
218 && krb5_config_get_bool (context, NULL,
222 context->kdc_sec_offset = rep->enc_part.authtime - sec_now;
223 krb5_timeofday (context, &sec_now);
226 /* check all times */
228 if (rep->enc_part.starttime) {
229 tmp_time = *rep->enc_part.starttime;
231 tmp_time = rep->enc_part.authtime;
233 if (creds->times.starttime == 0
234 && abs(tmp_time - sec_now) > context->max_skew) {
235 ret = KRB5KRB_AP_ERR_SKEW;
236 krb5_set_error_string (context,
237 "time skew (%d) larger than max (%d)",
238 abs(tmp_time - sec_now),
239 (int)context->max_skew);
243 if (creds->times.starttime != 0
244 && tmp_time != creds->times.starttime) {
245 krb5_clear_error_string (context);
246 ret = KRB5KRB_AP_ERR_MODIFIED;
250 creds->times.starttime = tmp_time;
252 if (rep->enc_part.renew_till) {
253 tmp_time = *rep->enc_part.renew_till;
257 if (creds->times.renew_till != 0
258 && tmp_time > creds->times.renew_till) {
259 krb5_clear_error_string (context);
260 ret = KRB5KRB_AP_ERR_MODIFIED;
264 creds->times.renew_till = tmp_time;
266 creds->times.authtime = rep->enc_part.authtime;
268 if (creds->times.endtime != 0
269 && rep->enc_part.endtime > creds->times.endtime) {
270 krb5_clear_error_string (context);
271 ret = KRB5KRB_AP_ERR_MODIFIED;
275 creds->times.endtime = rep->enc_part.endtime;
277 if(rep->enc_part.caddr)
278 krb5_copy_addresses (context, rep->enc_part.caddr, &creds->addresses);
280 krb5_copy_addresses (context, addrs, &creds->addresses);
282 creds->addresses.len = 0;
283 creds->addresses.val = NULL;
285 creds->flags.b = rep->enc_part.flags;
287 creds->authdata.len = 0;
288 creds->authdata.val = NULL;
289 creds->session.keyvalue.length = 0;
290 creds->session.keyvalue.data = NULL;
291 creds->session.keytype = rep->enc_part.key.keytype;
292 ret = krb5_data_copy (&creds->session.keyvalue,
293 rep->enc_part.key.keyvalue.data,
294 rep->enc_part.key.keyvalue.length);
297 memset (rep->enc_part.key.keyvalue.data, 0,
298 rep->enc_part.key.keyvalue.length);
303 static krb5_error_code
304 make_pa_enc_timestamp(krb5_context context, PA_DATA *pa,
305 krb5_enctype etype, krb5_keyblock *key)
311 EncryptedData encdata;
317 krb5_us_timeofday (context, &sec, &usec);
322 ASN1_MALLOC_ENCODE(PA_ENC_TS_ENC, buf, buf_size, &p, &len, ret);
326 krb5_abortx(context, "internal error in ASN.1 encoder");
327 ret = krb5_crypto_init(context, key, 0, &crypto);
332 ret = krb5_encrypt_EncryptedData(context,
334 KRB5_KU_PA_ENC_TIMESTAMP,
340 krb5_crypto_destroy(context, crypto);
344 ASN1_MALLOC_ENCODE(EncryptedData, buf, buf_size, &encdata, &len, ret);
345 free_EncryptedData(&encdata);
349 krb5_abortx(context, "internal error in ASN.1 encoder");
350 pa->padata_type = KRB5_PADATA_ENC_TIMESTAMP;
351 pa->padata_value.length = len;
352 pa->padata_value.data = buf;
356 static krb5_error_code
357 add_padata(krb5_context context,
359 krb5_principal client,
360 krb5_key_proc key_proc,
361 krb5_const_pointer keyseed,
362 krb5_enctype *enctypes,
373 /* default to standard salt */
374 ret = krb5_get_pw_salt (context, client, &salt2);
378 enctypes = context->etypes;
380 for (ep = enctypes; *ep != ETYPE_NULL; ep++)
383 pa2 = realloc (md->val, (md->len + netypes) * sizeof(*md->val));
385 krb5_set_error_string(context, "malloc: out of memory");
390 for (i = 0; i < netypes; ++i) {
393 ret = (*key_proc)(context, enctypes[i], *salt, keyseed, &key);
396 ret = make_pa_enc_timestamp (context, &md->val[md->len],
398 krb5_free_keyblock (context, key);
404 krb5_free_salt(context, salt2);
408 static krb5_error_code
409 init_as_req (krb5_context context,
412 const krb5_addresses *addrs,
413 const krb5_enctype *etypes,
414 const krb5_preauthtype *ptypes,
415 const krb5_preauthdata *preauth,
416 krb5_key_proc key_proc,
417 krb5_const_pointer keyseed,
424 memset(a, 0, sizeof(*a));
427 a->msg_type = krb_as_req;
428 a->req_body.kdc_options = opts.b;
429 a->req_body.cname = malloc(sizeof(*a->req_body.cname));
430 if (a->req_body.cname == NULL) {
432 krb5_set_error_string(context, "malloc: out of memory");
435 a->req_body.sname = malloc(sizeof(*a->req_body.sname));
436 if (a->req_body.sname == NULL) {
438 krb5_set_error_string(context, "malloc: out of memory");
441 ret = krb5_principal2principalname (a->req_body.cname, creds->client);
444 ret = krb5_principal2principalname (a->req_body.sname, creds->server);
447 ret = copy_Realm(&creds->client->realm, &a->req_body.realm);
451 if(creds->times.starttime) {
452 a->req_body.from = malloc(sizeof(*a->req_body.from));
453 if (a->req_body.from == NULL) {
455 krb5_set_error_string(context, "malloc: out of memory");
458 *a->req_body.from = creds->times.starttime;
460 if(creds->times.endtime){
461 ALLOC(a->req_body.till, 1);
462 *a->req_body.till = creds->times.endtime;
464 if(creds->times.renew_till){
465 a->req_body.rtime = malloc(sizeof(*a->req_body.rtime));
466 if (a->req_body.rtime == NULL) {
468 krb5_set_error_string(context, "malloc: out of memory");
471 *a->req_body.rtime = creds->times.renew_till;
473 a->req_body.nonce = nonce;
474 ret = krb5_init_etype (context,
475 &a->req_body.etype.len,
476 &a->req_body.etype.val,
482 * This means no addresses
485 if (addrs && addrs->len == 0) {
486 a->req_body.addresses = NULL;
488 a->req_body.addresses = malloc(sizeof(*a->req_body.addresses));
489 if (a->req_body.addresses == NULL) {
491 krb5_set_error_string(context, "malloc: out of memory");
496 ret = krb5_copy_addresses(context, addrs, a->req_body.addresses);
498 ret = krb5_get_all_client_addrs (context, a->req_body.addresses);
499 if(ret == 0 && a->req_body.addresses->len == 0) {
500 free(a->req_body.addresses);
501 a->req_body.addresses = NULL;
508 a->req_body.enc_authorization_data = NULL;
509 a->req_body.additional_tickets = NULL;
511 if(preauth != NULL) {
514 if(a->padata == NULL) {
516 krb5_set_error_string(context, "malloc: out of memory");
519 for(i = 0; i < preauth->len; i++) {
520 if(preauth->val[i].type == KRB5_PADATA_ENC_TIMESTAMP){
522 PA_DATA *tmp = realloc(a->padata->val,
524 preauth->val[i].info.len) *
525 sizeof(*a->padata->val));
528 krb5_set_error_string(context, "malloc: out of memory");
531 a->padata->val = tmp;
532 for(j = 0; j < preauth->val[i].info.len; j++) {
533 krb5_salt *sp = &salt;
534 if(preauth->val[i].info.val[j].salttype)
535 salt.salttype = *preauth->val[i].info.val[j].salttype;
537 salt.salttype = KRB5_PW_SALT;
538 if(preauth->val[i].info.val[j].salt)
539 salt.saltvalue = *preauth->val[i].info.val[j].salt;
541 if(salt.salttype == KRB5_PW_SALT)
544 krb5_data_zero(&salt.saltvalue);
545 ret = add_padata(context, a->padata, creds->client,
547 &preauth->val[i].info.val[j].etype, 1,
555 /* not sure this is the way to use `ptypes' */
556 if (ptypes == NULL || *ptypes == KRB5_PADATA_NONE)
558 else if (*ptypes == KRB5_PADATA_ENC_TIMESTAMP) {
560 if (a->padata == NULL) {
562 krb5_set_error_string(context, "malloc: out of memory");
566 a->padata->val = NULL;
568 /* make a v5 salted pa-data */
569 add_padata(context, a->padata, creds->client,
570 key_proc, keyseed, a->req_body.etype.val,
571 a->req_body.etype.len, NULL);
573 /* make a v4 salted pa-data */
574 salt.salttype = KRB5_PW_SALT;
575 krb5_data_zero(&salt.saltvalue);
576 add_padata(context, a->padata, creds->client,
577 key_proc, keyseed, a->req_body.etype.val,
578 a->req_body.etype.len, &salt);
580 krb5_set_error_string (context, "pre-auth type %d not supported",
582 ret = KRB5_PREAUTH_BAD_TYPE;
592 set_ptypes(krb5_context context,
594 krb5_preauthtype **ptypes,
595 krb5_preauthdata **preauth)
597 static krb5_preauthdata preauth2;
598 static krb5_preauthtype ptypes2[] = { KRB5_PADATA_ENC_TIMESTAMP, KRB5_PADATA_NONE };
603 decode_METHOD_DATA(error->e_data->data,
604 error->e_data->length,
607 for(i = 0; i < md.len; i++){
608 switch(md.val[i].padata_type){
609 case KRB5_PADATA_ENC_TIMESTAMP:
612 case KRB5_PADATA_ETYPE_INFO:
613 *preauth = &preauth2;
614 ALLOC_SEQ(*preauth, 1);
615 (*preauth)->val[0].type = KRB5_PADATA_ENC_TIMESTAMP;
616 krb5_decode_ETYPE_INFO(context,
617 md.val[i].padata_value.data,
618 md.val[i].padata_value.length,
619 &(*preauth)->val[0].info,
626 free_METHOD_DATA(&md);
634 krb5_get_in_cred(krb5_context context,
636 const krb5_addresses *addrs,
637 const krb5_enctype *etypes,
638 const krb5_preauthtype *ptypes,
639 const krb5_preauthdata *preauth,
640 krb5_key_proc key_proc,
641 krb5_const_pointer keyseed,
642 krb5_decrypt_proc decrypt_proc,
643 krb5_const_pointer decryptarg,
645 krb5_kdc_rep *ret_as_reply)
658 krb5_preauthdata *my_preauth = NULL;
664 krb5_generate_random_block (&nonce, sizeof(nonce));
669 ret = init_as_req (context,
681 free_ETYPE_INFO(&my_preauth->val[0].info);
682 free (my_preauth->val);
687 ASN1_MALLOC_ENCODE(AS_REQ, req.data, req.length, &a, &len, ret);
691 if(len != req.length)
692 krb5_abortx(context, "internal error in ASN.1 encoder");
694 ret = krb5_sendto_kdc (context, &req, &creds->client->realm, &resp);
695 krb5_data_free(&req);
699 memset (&rep, 0, sizeof(rep));
700 ret = decode_AS_REP(resp.data, resp.length, &rep.kdc_rep, &size);
702 /* let's try to parse it as a KRB-ERROR */
706 ret2 = krb5_rd_error(context, &resp, &error);
707 if(ret2 && resp.data && ((char*)resp.data)[0] == 4)
708 ret = KRB5KRB_AP_ERR_V4_REPLY;
709 krb5_data_free(&resp);
711 ret = krb5_error_from_rd_error(context, &error, creds);
712 /* if no preauth was set and KDC requires it, give it
714 if (!ptypes && !preauth
715 && ret == KRB5KDC_ERR_PREAUTH_REQUIRED
717 || ret == KRB5KDC_ERR_BADOPTION
719 && set_ptypes(context, &error, &ptypes, &my_preauth)) {
721 preauth = my_preauth;
722 krb5_free_error_contents(context, &error);
723 krb5_clear_error_string(context);
727 ret_as_reply->error = error;
729 free_KRB_ERROR (&error);
734 krb5_data_free(&resp);
738 etype = rep.kdc_rep.enc_part.etype;
739 if(rep.kdc_rep.padata){
741 pa = krb5_find_padata(rep.kdc_rep.padata->val, rep.kdc_rep.padata->len,
742 KRB5_PADATA_PW_SALT, &index);
745 pa = krb5_find_padata(rep.kdc_rep.padata->val,
746 rep.kdc_rep.padata->len,
747 KRB5_PADATA_AFS3_SALT, &index);
751 salt.salttype = pa->padata_type;
752 salt.saltvalue = pa->padata_value;
754 ret = (*key_proc)(context, etype, salt, keyseed, &key);
756 /* make a v5 salted pa-data */
757 ret = krb5_get_pw_salt (context, creds->client, &salt);
761 ret = (*key_proc)(context, etype, salt, keyseed, &key);
762 krb5_free_salt(context, salt);
767 ret = _krb5_extract_ticket(context,
772 KRB5_KU_AS_REP_ENC_PART,
776 opts.b.request_anonymous,
779 memset (key->keyvalue.data, 0, key->keyvalue.length);
780 krb5_free_keyblock_contents (context, key);
784 if (ret == 0 && ret_as_reply)
787 krb5_free_kdc_rep (context, &rep);
792 krb5_get_in_tkt(krb5_context context,
794 const krb5_addresses *addrs,
795 const krb5_enctype *etypes,
796 const krb5_preauthtype *ptypes,
797 krb5_key_proc key_proc,
798 krb5_const_pointer keyseed,
799 krb5_decrypt_proc decrypt_proc,
800 krb5_const_pointer decryptarg,
803 krb5_kdc_rep *ret_as_reply)
808 opts.b = int2KDCOptions(options);
810 ret = krb5_get_in_cred (context,
825 ret = krb5_cc_store_cred (context, ccache, creds);