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
36 RCSID("$Id: kerberos5.c 22071 2007-11-14 20:04:50Z lha $");
38 #define MAX_TIME ((time_t)((1U << 31) - 1))
41 _kdc_fix_time(time_t **t)
47 if(**t == 0) **t = MAX_TIME; /* fix for old clients */
51 realloc_method_data(METHOD_DATA *md)
54 pa = realloc(md->val, (md->len + 1) * sizeof(*md->val));
63 set_salt_padata (METHOD_DATA *md, Salt *salt)
66 realloc_method_data(md);
67 md->val[md->len - 1].padata_type = salt->type;
68 der_copy_octet_string(&salt->salt,
69 &md->val[md->len - 1].padata_value);
74 _kdc_find_padata(const KDC_REQ *req, int *start, int type)
76 if (req->padata == NULL)
79 while(*start < req->padata->len){
81 if(req->padata->val[*start - 1].padata_type == type)
82 return &req->padata->val[*start - 1];
88 * Detect if `key' is the using the the precomputed `default_salt'.
92 is_default_salt_p(const krb5_salt *default_salt, const Key *key)
94 if (key->salt == NULL)
96 if (default_salt->salttype != key->salt->type)
98 if (krb5_data_cmp(&default_salt->saltvalue, &key->salt->salt))
104 * return the first appropriate key of `princ' in `ret_key'. Look for
105 * all the etypes in (`etypes', `len'), stopping as soon as we find
106 * one, but preferring one that has default salt
110 _kdc_find_etype(krb5_context context, const hdb_entry_ex *princ,
111 krb5_enctype *etypes, unsigned len,
112 Key **ret_key, krb5_enctype *ret_etype)
115 krb5_error_code ret = KRB5KDC_ERR_ETYPE_NOSUPP;
118 krb5_get_pw_salt (context, princ->entry.principal, &def_salt);
120 for(i = 0; ret != 0 && i < len ; i++) {
123 if (krb5_enctype_valid(context, etypes[i]) != 0)
126 while (hdb_next_enctype2key(context, &princ->entry, etypes[i], &key) == 0) {
127 if (key->key.keyvalue.length == 0) {
128 ret = KRB5KDC_ERR_NULL_KEY;
132 *ret_etype = etypes[i];
134 if (is_default_salt_p(&def_salt, key)) {
135 krb5_free_salt (context, def_salt);
140 krb5_free_salt (context, def_salt);
145 _kdc_make_anonymous_principalname (PrincipalName *pn)
147 pn->name_type = KRB5_NT_PRINCIPAL;
148 pn->name_string.len = 1;
149 pn->name_string.val = malloc(sizeof(*pn->name_string.val));
150 if (pn->name_string.val == NULL)
152 pn->name_string.val[0] = strdup("anonymous");
153 if (pn->name_string.val[0] == NULL) {
154 free(pn->name_string.val);
155 pn->name_string.val = NULL;
162 _kdc_log_timestamp(krb5_context context,
163 krb5_kdc_configuration *config,
165 KerberosTime authtime, KerberosTime *starttime,
166 KerberosTime endtime, KerberosTime *renew_till)
168 char authtime_str[100], starttime_str[100],
169 endtime_str[100], renewtime_str[100];
171 krb5_format_time(context, authtime,
172 authtime_str, sizeof(authtime_str), TRUE);
174 krb5_format_time(context, *starttime,
175 starttime_str, sizeof(starttime_str), TRUE);
177 strlcpy(starttime_str, "unset", sizeof(starttime_str));
178 krb5_format_time(context, endtime,
179 endtime_str, sizeof(endtime_str), TRUE);
181 krb5_format_time(context, *renew_till,
182 renewtime_str, sizeof(renewtime_str), TRUE);
184 strlcpy(renewtime_str, "unset", sizeof(renewtime_str));
186 kdc_log(context, config, 5,
187 "%s authtime: %s starttime: %s endtime: %s renew till: %s",
188 type, authtime_str, starttime_str, endtime_str, renewtime_str);
192 log_patypes(krb5_context context,
193 krb5_kdc_configuration *config,
196 struct rk_strpool *p = NULL;
200 for (i = 0; i < padata->len; i++) {
201 switch(padata->val[i].padata_type) {
202 case KRB5_PADATA_PK_AS_REQ:
203 p = rk_strpoolprintf(p, "PK-INIT(ietf)");
205 case KRB5_PADATA_PK_AS_REQ_WIN:
206 p = rk_strpoolprintf(p, "PK-INIT(win2k)");
208 case KRB5_PADATA_PA_PK_OCSP_RESPONSE:
209 p = rk_strpoolprintf(p, "OCSP");
211 case KRB5_PADATA_ENC_TIMESTAMP:
212 p = rk_strpoolprintf(p, "encrypted-timestamp");
215 p = rk_strpoolprintf(p, "%d", padata->val[i].padata_type);
218 if (p && i + 1 < padata->len)
219 p = rk_strpoolprintf(p, ", ");
221 kdc_log(context, config, 0, "out of memory");
226 p = rk_strpoolprintf(p, "none");
228 str = rk_strpoolcollect(p);
229 kdc_log(context, config, 0, "Client sent patypes: %s", str);
239 _kdc_encode_reply(krb5_context context,
240 krb5_kdc_configuration *config,
241 KDC_REP *rep, const EncTicketPart *et, EncKDCRepPart *ek,
243 int skvno, const EncryptionKey *skey,
244 int ckvno, const EncryptionKey *ckey,
254 ASN1_MALLOC_ENCODE(EncTicketPart, buf, buf_size, et, &len, ret);
256 kdc_log(context, config, 0, "Failed to encode ticket: %s",
257 krb5_get_err_text(context, ret));
260 if(buf_size != len) {
262 kdc_log(context, config, 0, "Internal error in ASN.1 encoder");
263 *e_text = "KDC internal error";
264 return KRB5KRB_ERR_GENERIC;
267 ret = krb5_crypto_init(context, skey, etype, &crypto);
270 kdc_log(context, config, 0, "krb5_crypto_init failed: %s",
271 krb5_get_err_text(context, ret));
275 ret = krb5_encrypt_EncryptedData(context,
281 &rep->ticket.enc_part);
283 krb5_crypto_destroy(context, crypto);
285 kdc_log(context, config, 0, "Failed to encrypt data: %s",
286 krb5_get_err_text(context, ret));
290 if(rep->msg_type == krb_as_rep && !config->encode_as_rep_as_tgs_rep)
291 ASN1_MALLOC_ENCODE(EncASRepPart, buf, buf_size, ek, &len, ret);
293 ASN1_MALLOC_ENCODE(EncTGSRepPart, buf, buf_size, ek, &len, ret);
295 kdc_log(context, config, 0, "Failed to encode KDC-REP: %s",
296 krb5_get_err_text(context, ret));
299 if(buf_size != len) {
301 kdc_log(context, config, 0, "Internal error in ASN.1 encoder");
302 *e_text = "KDC internal error";
303 return KRB5KRB_ERR_GENERIC;
305 ret = krb5_crypto_init(context, ckey, 0, &crypto);
308 kdc_log(context, config, 0, "krb5_crypto_init failed: %s",
309 krb5_get_err_text(context, ret));
312 if(rep->msg_type == krb_as_rep) {
313 krb5_encrypt_EncryptedData(context,
315 KRB5_KU_AS_REP_ENC_PART,
321 ASN1_MALLOC_ENCODE(AS_REP, buf, buf_size, rep, &len, ret);
323 krb5_encrypt_EncryptedData(context,
325 KRB5_KU_TGS_REP_ENC_PART_SESSION,
331 ASN1_MALLOC_ENCODE(TGS_REP, buf, buf_size, rep, &len, ret);
333 krb5_crypto_destroy(context, crypto);
335 kdc_log(context, config, 0, "Failed to encode KDC-REP: %s",
336 krb5_get_err_text(context, ret));
339 if(buf_size != len) {
341 kdc_log(context, config, 0, "Internal error in ASN.1 encoder");
342 *e_text = "KDC internal error";
343 return KRB5KRB_ERR_GENERIC;
346 reply->length = buf_size;
351 * Return 1 if the client have only older enctypes, this is for
352 * determining if the server should send ETYPE_INFO2 or not.
356 older_enctype(krb5_enctype enctype)
359 case ETYPE_DES_CBC_CRC:
360 case ETYPE_DES_CBC_MD4:
361 case ETYPE_DES_CBC_MD5:
362 case ETYPE_DES3_CBC_SHA1:
363 case ETYPE_ARCFOUR_HMAC_MD5:
364 case ETYPE_ARCFOUR_HMAC_MD5_56:
366 * The following three is "old" windows enctypes and is needed for
367 * windows 2000 hosts.
369 case ETYPE_ARCFOUR_MD4:
370 case ETYPE_ARCFOUR_HMAC_OLD:
371 case ETYPE_ARCFOUR_HMAC_OLD_EXP:
379 only_older_enctype_p(const KDC_REQ *req)
383 for(i = 0; i < req->req_body.etype.len; i++) {
384 if (!older_enctype(req->req_body.etype.val[i]))
394 static krb5_error_code
395 make_etype_info_entry(krb5_context context, ETYPE_INFO_ENTRY *ent, Key *key)
397 ent->etype = key->key.keytype;
400 ALLOC(ent->salttype);
402 if(key->salt->type == hdb_pw_salt)
403 *ent->salttype = 0; /* or 1? or NULL? */
404 else if(key->salt->type == hdb_afs3_salt)
407 kdc_log(context, config, 0, "unknown salt-type: %d",
409 return KRB5KRB_ERR_GENERIC;
411 /* according to `the specs', we can't send a salt if
412 we have AFS3 salted key, but that requires that you
413 *know* what cell you are using (e.g by assuming
414 that the cell is the same as the realm in lower
417 ALLOC(ent->salttype);
418 *ent->salttype = key->salt->type;
421 * We shouldn't sent salttype since it is incompatible with the
422 * specification and it breaks windows clients. The afs
423 * salting problem is solved by using KRB5-PADATA-AFS3-SALT
424 * implemented in Heimdal 0.7 and later.
426 ent->salttype = NULL;
428 krb5_copy_data(context, &key->salt->salt,
431 /* we return no salt type at all, as that should indicate
432 * the default salt type and make everybody happy. some
433 * systems (like w2k) dislike being told the salt type
436 ent->salttype = NULL;
442 static krb5_error_code
443 get_pa_etype_info(krb5_context context,
444 krb5_kdc_configuration *config,
445 METHOD_DATA *md, hdb_entry *client,
446 ENCTYPE *etypes, unsigned int etypes_len)
448 krb5_error_code ret = 0;
456 pa.len = client->keys.len;
457 if(pa.len > UINT_MAX/sizeof(*pa.val))
459 pa.val = malloc(pa.len * sizeof(*pa.val));
462 memset(pa.val, 0, pa.len * sizeof(*pa.val));
464 for(i = 0; i < client->keys.len; i++) {
465 for (j = 0; j < n; j++)
466 if (pa.val[j].etype == client->keys.val[i].key.keytype)
468 for(j = 0; j < etypes_len; j++) {
469 if(client->keys.val[i].key.keytype == etypes[j]) {
470 if (krb5_enctype_valid(context, etypes[j]) != 0)
472 if (!older_enctype(etypes[j]))
475 krb5_abortx(context, "internal error: n >= p.len");
476 if((ret = make_etype_info_entry(context,
478 &client->keys.val[i])) != 0) {
479 free_ETYPE_INFO(&pa);
487 for(i = 0; i < client->keys.len; i++) {
489 for(j = 0; j < etypes_len; j++) {
490 if(client->keys.val[i].key.keytype == etypes[j])
493 if (krb5_enctype_valid(context, client->keys.val[i].key.keytype) != 0)
495 if (!older_enctype(etypes[j]))
498 krb5_abortx(context, "internal error: n >= p.len");
499 if((ret = make_etype_info_entry(context,
501 &client->keys.val[i])) != 0) {
502 free_ETYPE_INFO(&pa);
509 /* stripped out dups, newer enctypes, and not valid enctypes */
513 ASN1_MALLOC_ENCODE(ETYPE_INFO, buf, len, &pa, &len, ret);
514 free_ETYPE_INFO(&pa);
517 ret = realloc_method_data(md);
522 md->val[md->len - 1].padata_type = KRB5_PADATA_ETYPE_INFO;
523 md->val[md->len - 1].padata_value.length = len;
524 md->val[md->len - 1].padata_value.data = buf;
532 extern int _krb5_AES_string_to_default_iterator;
534 static krb5_error_code
535 make_etype_info2_entry(ETYPE_INFO2_ENTRY *ent, Key *key)
537 ent->etype = key->key.keytype;
540 if (ent->salt == NULL)
542 *ent->salt = malloc(key->salt->salt.length + 1);
543 if (*ent->salt == NULL) {
548 memcpy(*ent->salt, key->salt->salt.data, key->salt->salt.length);
549 (*ent->salt)[key->salt->salt.length] = '\0';
553 ent->s2kparams = NULL;
555 switch (key->key.keytype) {
556 case ETYPE_AES128_CTS_HMAC_SHA1_96:
557 case ETYPE_AES256_CTS_HMAC_SHA1_96:
558 ALLOC(ent->s2kparams);
559 if (ent->s2kparams == NULL)
561 ent->s2kparams->length = 4;
562 ent->s2kparams->data = malloc(ent->s2kparams->length);
563 if (ent->s2kparams->data == NULL) {
564 free(ent->s2kparams);
565 ent->s2kparams = NULL;
568 _krb5_put_int(ent->s2kparams->data,
569 _krb5_AES_string_to_default_iterator,
570 ent->s2kparams->length);
572 case ETYPE_DES_CBC_CRC:
573 case ETYPE_DES_CBC_MD4:
574 case ETYPE_DES_CBC_MD5:
575 /* Check if this was a AFS3 salted key */
576 if(key->salt && key->salt->type == hdb_afs3_salt){
577 ALLOC(ent->s2kparams);
578 if (ent->s2kparams == NULL)
580 ent->s2kparams->length = 1;
581 ent->s2kparams->data = malloc(ent->s2kparams->length);
582 if (ent->s2kparams->data == NULL) {
583 free(ent->s2kparams);
584 ent->s2kparams = NULL;
587 _krb5_put_int(ent->s2kparams->data,
589 ent->s2kparams->length);
599 * Return an ETYPE-INFO2. Enctypes are storted the same way as in the
600 * database (client supported enctypes first, then the unsupported
604 static krb5_error_code
605 get_pa_etype_info2(krb5_context context,
606 krb5_kdc_configuration *config,
607 METHOD_DATA *md, hdb_entry *client,
608 ENCTYPE *etypes, unsigned int etypes_len)
610 krb5_error_code ret = 0;
617 pa.len = client->keys.len;
618 if(pa.len > UINT_MAX/sizeof(*pa.val))
620 pa.val = malloc(pa.len * sizeof(*pa.val));
623 memset(pa.val, 0, pa.len * sizeof(*pa.val));
625 for(i = 0; i < client->keys.len; i++) {
626 for (j = 0; j < n; j++)
627 if (pa.val[j].etype == client->keys.val[i].key.keytype)
629 for(j = 0; j < etypes_len; j++) {
630 if(client->keys.val[i].key.keytype == etypes[j]) {
631 if (krb5_enctype_valid(context, etypes[j]) != 0)
634 krb5_abortx(context, "internal error: n >= p.len");
635 if((ret = make_etype_info2_entry(&pa.val[n++],
636 &client->keys.val[i])) != 0) {
637 free_ETYPE_INFO2(&pa);
645 /* send enctypes that the client doesn't know about too */
646 for(i = 0; i < client->keys.len; i++) {
648 for(j = 0; j < etypes_len; j++) {
649 if(client->keys.val[i].key.keytype == etypes[j])
652 if (krb5_enctype_valid(context, client->keys.val[i].key.keytype) != 0)
655 krb5_abortx(context, "internal error: n >= p.len");
656 if((ret = make_etype_info2_entry(&pa.val[n++],
657 &client->keys.val[i])) != 0) {
658 free_ETYPE_INFO2(&pa);
665 /* stripped out dups, and not valid enctypes */
669 ASN1_MALLOC_ENCODE(ETYPE_INFO2, buf, len, &pa, &len, ret);
670 free_ETYPE_INFO2(&pa);
673 ret = realloc_method_data(md);
678 md->val[md->len - 1].padata_type = KRB5_PADATA_ETYPE_INFO2;
679 md->val[md->len - 1].padata_value.length = len;
680 md->val[md->len - 1].padata_value.data = buf;
689 log_as_req(krb5_context context,
690 krb5_kdc_configuration *config,
693 const KDC_REQ_BODY *b)
696 struct rk_strpool *p = NULL;
700 for (i = 0; i < b->etype.len; i++) {
701 ret = krb5_enctype_to_string(context, b->etype.val[i], &str);
703 p = rk_strpoolprintf(p, "%s", str);
706 p = rk_strpoolprintf(p, "%d", b->etype.val[i]);
707 if (p && i + 1 < b->etype.len)
708 p = rk_strpoolprintf(p, ", ");
710 kdc_log(context, config, 0, "out of memory");
715 p = rk_strpoolprintf(p, "no encryption types");
717 str = rk_strpoolcollect(p);
718 kdc_log(context, config, 0, "Client supported enctypes: %s", str);
725 ret = krb5_enctype_to_string(context, cetype, &cet);
727 ret = krb5_enctype_to_string(context, setype, &set);
729 kdc_log(context, config, 5, "Using %s/%s", cet, set);
735 kdc_log(context, config, 5, "Using e-types %d/%d", cetype, setype);
740 unparse_flags(KDCOptions2int(b->kdc_options), asn1_KDCOptions_units(),
741 fixedstr, sizeof(fixedstr));
743 kdc_log(context, config, 2, "Requested flags: %s", fixedstr);
748 * verify the flags on `client' and `server', returning 0
749 * if they are OK and generating an error messages and returning
750 * and error code otherwise.
754 _kdc_check_flags(krb5_context context,
755 krb5_kdc_configuration *config,
756 hdb_entry_ex *client_ex, const char *client_name,
757 hdb_entry_ex *server_ex, const char *server_name,
758 krb5_boolean is_as_req)
760 if(client_ex != NULL) {
761 hdb_entry *client = &client_ex->entry;
764 if (client->flags.invalid) {
765 kdc_log(context, config, 0,
766 "Client (%s) has invalid bit set", client_name);
767 return KRB5KDC_ERR_POLICY;
770 if(!client->flags.client){
771 kdc_log(context, config, 0,
772 "Principal may not act as client -- %s", client_name);
773 return KRB5KDC_ERR_POLICY;
776 if (client->valid_start && *client->valid_start > kdc_time) {
777 char starttime_str[100];
778 krb5_format_time(context, *client->valid_start,
779 starttime_str, sizeof(starttime_str), TRUE);
780 kdc_log(context, config, 0,
781 "Client not yet valid until %s -- %s",
782 starttime_str, client_name);
783 return KRB5KDC_ERR_CLIENT_NOTYET;
786 if (client->valid_end && *client->valid_end < kdc_time) {
787 char endtime_str[100];
788 krb5_format_time(context, *client->valid_end,
789 endtime_str, sizeof(endtime_str), TRUE);
790 kdc_log(context, config, 0,
791 "Client expired at %s -- %s",
792 endtime_str, client_name);
793 return KRB5KDC_ERR_NAME_EXP;
796 if (client->pw_end && *client->pw_end < kdc_time
797 && (server_ex == NULL || !server_ex->entry.flags.change_pw)) {
799 krb5_format_time(context, *client->pw_end,
800 pwend_str, sizeof(pwend_str), TRUE);
801 kdc_log(context, config, 0,
802 "Client's key has expired at %s -- %s",
803 pwend_str, client_name);
804 return KRB5KDC_ERR_KEY_EXPIRED;
810 if (server_ex != NULL) {
811 hdb_entry *server = &server_ex->entry;
813 if (server->flags.invalid) {
814 kdc_log(context, config, 0,
815 "Server has invalid flag set -- %s", server_name);
816 return KRB5KDC_ERR_POLICY;
819 if(!server->flags.server){
820 kdc_log(context, config, 0,
821 "Principal may not act as server -- %s", server_name);
822 return KRB5KDC_ERR_POLICY;
825 if(!is_as_req && server->flags.initial) {
826 kdc_log(context, config, 0,
827 "AS-REQ is required for server -- %s", server_name);
828 return KRB5KDC_ERR_POLICY;
831 if (server->valid_start && *server->valid_start > kdc_time) {
832 char starttime_str[100];
833 krb5_format_time(context, *server->valid_start,
834 starttime_str, sizeof(starttime_str), TRUE);
835 kdc_log(context, config, 0,
836 "Server not yet valid until %s -- %s",
837 starttime_str, server_name);
838 return KRB5KDC_ERR_SERVICE_NOTYET;
841 if (server->valid_end && *server->valid_end < kdc_time) {
842 char endtime_str[100];
843 krb5_format_time(context, *server->valid_end,
844 endtime_str, sizeof(endtime_str), TRUE);
845 kdc_log(context, config, 0,
846 "Server expired at %s -- %s",
847 endtime_str, server_name);
848 return KRB5KDC_ERR_SERVICE_EXP;
851 if (server->pw_end && *server->pw_end < kdc_time) {
853 krb5_format_time(context, *server->pw_end,
854 pwend_str, sizeof(pwend_str), TRUE);
855 kdc_log(context, config, 0,
856 "Server's key has expired at -- %s",
857 pwend_str, server_name);
858 return KRB5KDC_ERR_KEY_EXPIRED;
865 * Return TRUE if `from' is part of `addresses' taking into consideration
866 * the configuration variables that tells us how strict we should be about
871 _kdc_check_addresses(krb5_context context,
872 krb5_kdc_configuration *config,
873 HostAddresses *addresses, const struct sockaddr *from)
878 krb5_boolean only_netbios = TRUE;
881 if(config->check_ticket_addresses == 0)
884 if(addresses == NULL)
885 return config->allow_null_ticket_addresses;
887 for (i = 0; i < addresses->len; ++i) {
888 if (addresses->val[i].addr_type != KRB5_ADDRESS_NETBIOS) {
889 only_netbios = FALSE;
893 /* Windows sends it's netbios name, which I can only assume is
894 * used for the 'allowed workstations' check. This is painful,
895 * but we still want to check IP addresses if they happen to be
900 return config->allow_null_ticket_addresses;
902 ret = krb5_sockaddr2address (context, from, &addr);
906 result = krb5_address_search(context, &addr, addresses);
907 krb5_free_address (context, &addr);
916 send_pac_p(krb5_context context, KDC_REQ *req)
919 PA_PAC_REQUEST pacreq;
923 pa = _kdc_find_padata(req, &i, KRB5_PADATA_PA_PAC_REQUEST);
927 ret = decode_PA_PAC_REQUEST(pa->padata_value.data,
928 pa->padata_value.length,
933 i = pacreq.include_pac;
934 free_PA_PAC_REQUEST(&pacreq);
945 _kdc_as_rep(krb5_context context,
946 krb5_kdc_configuration *config,
948 const krb5_data *req_buffer,
951 struct sockaddr *from_addr,
954 KDC_REQ_BODY *b = &req->req_body;
956 KDCOptions f = b->kdc_options;
957 hdb_entry_ex *client = NULL, *server = NULL;
958 krb5_enctype cetype, setype, sessionetype;
962 krb5_principal client_princ = NULL, server_princ = NULL;
963 char *client_name = NULL, *server_name = NULL;
964 krb5_error_code ret = 0;
965 const char *e_text = NULL;
968 EncryptionKey *reply_key;
971 pk_client_params *pkp = NULL;
974 memset(&rep, 0, sizeof(rep));
975 krb5_data_zero(&e_data);
978 flags |= HDB_F_CANON;
980 if(b->sname == NULL){
981 ret = KRB5KRB_ERR_GENERIC;
982 e_text = "No server in request";
984 ret = _krb5_principalname2krb5_principal (context,
989 ret = krb5_unparse_name(context, server_princ, &server_name);
992 kdc_log(context, config, 0,
993 "AS-REQ malformed server name from %s", from);
997 if(b->cname == NULL){
998 ret = KRB5KRB_ERR_GENERIC;
999 e_text = "No client in request";
1002 if (b->cname->name_type == KRB5_NT_ENTERPRISE_PRINCIPAL) {
1003 if (b->cname->name_string.len != 1) {
1004 kdc_log(context, config, 0,
1005 "AS-REQ malformed canon request from %s, "
1006 "enterprise name with %d name components",
1007 from, b->cname->name_string.len);
1008 ret = KRB5_PARSE_MALFORMED;
1011 ret = krb5_parse_name(context, b->cname->name_string.val[0],
1016 ret = _krb5_principalname2krb5_principal (context,
1023 ret = krb5_unparse_name(context, client_princ, &client_name);
1026 kdc_log(context, config, 0,
1027 "AS-REQ malformed client name from %s", from);
1031 kdc_log(context, config, 0, "AS-REQ %s from %s for %s",
1032 client_name, from, server_name);
1034 ret = _kdc_db_fetch(context, config, client_princ,
1035 HDB_F_GET_CLIENT | flags, NULL, &client);
1037 kdc_log(context, config, 0, "UNKNOWN -- %s: %s", client_name,
1038 krb5_get_err_text(context, ret));
1039 ret = KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN;
1043 ret = _kdc_db_fetch(context, config, server_princ,
1044 HDB_F_GET_SERVER|HDB_F_GET_KRBTGT,
1047 kdc_log(context, config, 0, "UNKNOWN -- %s: %s", server_name,
1048 krb5_get_err_text(context, ret));
1049 ret = KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN;
1053 ret = _kdc_windc_client_access(context, client, req);
1057 ret = _kdc_check_flags(context, config,
1058 client, client_name,
1059 server, server_name,
1064 memset(&et, 0, sizeof(et));
1065 memset(&ek, 0, sizeof(ek));
1072 log_patypes(context, config, req->padata);
1075 kdc_log(context, config, 5,
1076 "Looking for PKINIT pa-data -- %s", client_name);
1078 e_text = "No PKINIT PA found";
1081 if ((pa = _kdc_find_padata(req, &i, KRB5_PADATA_PK_AS_REQ)))
1085 if((pa = _kdc_find_padata(req, &i, KRB5_PADATA_PK_AS_REQ_WIN)))
1089 char *client_cert = NULL;
1091 ret = _kdc_pk_rd_padata(context, config, req, pa, &pkp);
1093 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
1094 kdc_log(context, config, 5,
1095 "Failed to decode PKINIT PA-DATA -- %s",
1099 if (ret == 0 && pkp == NULL)
1102 ret = _kdc_pk_check_client(context,
1108 e_text = "PKINIT certificate not allowed to "
1109 "impersonate principal";
1110 _kdc_pk_free_client_param(context, pkp);
1112 kdc_log(context, config, 0, "%s", e_text);
1117 et.flags.pre_authent = 1;
1118 kdc_log(context, config, 0,
1119 "PKINIT pre-authentication succeeded -- %s using %s",
1120 client_name, client_cert);
1127 kdc_log(context, config, 5, "Looking for ENC-TS pa-data -- %s",
1131 e_text = "No ENC-TS found";
1132 while((pa = _kdc_find_padata(req, &i, KRB5_PADATA_ENC_TIMESTAMP))){
1136 EncryptedData enc_data;
1142 ret = decode_EncryptedData(pa->padata_value.data,
1143 pa->padata_value.length,
1147 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
1148 kdc_log(context, config, 5, "Failed to decode PA-DATA -- %s",
1153 ret = hdb_enctype2key(context, &client->entry,
1154 enc_data.etype, &pa_key);
1157 e_text = "No key matches pa-data";
1158 ret = KRB5KDC_ERR_ETYPE_NOSUPP;
1159 if(krb5_enctype_to_string(context, enc_data.etype, &estr))
1162 kdc_log(context, config, 5,
1163 "No client key matching pa-data (%d) -- %s",
1164 enc_data.etype, client_name);
1166 kdc_log(context, config, 5,
1167 "No client key matching pa-data (%s) -- %s",
1171 free_EncryptedData(&enc_data);
1176 ret = krb5_crypto_init(context, &pa_key->key, 0, &crypto);
1178 kdc_log(context, config, 0, "krb5_crypto_init failed: %s",
1179 krb5_get_err_text(context, ret));
1180 free_EncryptedData(&enc_data);
1184 ret = krb5_decrypt_EncryptedData (context,
1186 KRB5_KU_PA_ENC_TIMESTAMP,
1189 krb5_crypto_destroy(context, crypto);
1191 krb5_error_code ret2;
1192 ret2 = krb5_enctype_to_string(context,
1193 pa_key->key.keytype, &str);
1196 kdc_log(context, config, 5,
1197 "Failed to decrypt PA-DATA -- %s "
1198 "(enctype %s) error %s",
1200 str ? str : "unknown enctype",
1201 krb5_get_err_text(context, ret));
1204 if(hdb_next_enctype2key(context, &client->entry,
1205 enc_data.etype, &pa_key) == 0)
1207 e_text = "Failed to decrypt PA-DATA";
1209 free_EncryptedData(&enc_data);
1210 ret = KRB5KDC_ERR_PREAUTH_FAILED;
1213 free_EncryptedData(&enc_data);
1214 ret = decode_PA_ENC_TS_ENC(ts_data.data,
1218 krb5_data_free(&ts_data);
1220 e_text = "Failed to decode PA-ENC-TS-ENC";
1221 ret = KRB5KDC_ERR_PREAUTH_FAILED;
1222 kdc_log(context, config,
1223 5, "Failed to decode PA-ENC-TS_ENC -- %s",
1227 free_PA_ENC_TS_ENC(&p);
1228 if (abs(kdc_time - p.patimestamp) > context->max_skew) {
1229 char client_time[100];
1231 krb5_format_time(context, p.patimestamp,
1232 client_time, sizeof(client_time), TRUE);
1234 ret = KRB5KRB_AP_ERR_SKEW;
1235 kdc_log(context, config, 0,
1236 "Too large time skew, "
1237 "client time %s is out by %u > %u seconds -- %s",
1239 (unsigned)abs(kdc_time - p.patimestamp),
1243 /* This code is from samba, needs testing */
1245 * the following is needed to make windows clients
1246 * to retry using the timestamp in the error message
1248 * this is maybe a bug in windows to not trying when e_text
1253 e_text = "Too large time skew";
1257 et.flags.pre_authent = 1;
1259 ret = krb5_enctype_to_string(context,pa_key->key.keytype, &str);
1263 kdc_log(context, config, 2,
1264 "ENC-TS Pre-authentication succeeded -- %s using %s",
1265 client_name, str ? str : "unknown enctype");
1272 if(found_pa == 0 && config->require_preauth)
1274 /* We come here if we found a pa-enc-timestamp, but if there
1275 was some problem with it, other than too large skew */
1276 if(found_pa && et.flags.pre_authent == 0){
1277 kdc_log(context, config, 0, "%s -- %s", e_text, client_name);
1281 }else if (config->require_preauth
1282 || client->entry.flags.require_preauth
1283 || server->entry.flags.require_preauth) {
1284 METHOD_DATA method_data;
1290 method_data.len = 0;
1291 method_data.val = NULL;
1293 ret = realloc_method_data(&method_data);
1294 pa = &method_data.val[method_data.len-1];
1295 pa->padata_type = KRB5_PADATA_ENC_TIMESTAMP;
1296 pa->padata_value.length = 0;
1297 pa->padata_value.data = NULL;
1300 ret = realloc_method_data(&method_data);
1301 pa = &method_data.val[method_data.len-1];
1302 pa->padata_type = KRB5_PADATA_PK_AS_REQ;
1303 pa->padata_value.length = 0;
1304 pa->padata_value.data = NULL;
1306 ret = realloc_method_data(&method_data);
1307 pa = &method_data.val[method_data.len-1];
1308 pa->padata_type = KRB5_PADATA_PK_AS_REQ_WIN;
1309 pa->padata_value.length = 0;
1310 pa->padata_value.data = NULL;
1315 * - If the client only knows about old enctypes, then send
1316 * both info replies (we send 'info' first in the list).
1317 * - If the client is 'modern', because it knows about 'new'
1318 * enctype types, then only send the 'info2' reply.
1322 if (only_older_enctype_p(req))
1323 ret = get_pa_etype_info(context, config,
1324 &method_data, &client->entry,
1325 b->etype.val, b->etype.len);
1327 ret = get_pa_etype_info2(context, config, &method_data,
1328 &client->entry, b->etype.val, b->etype.len);
1331 ASN1_MALLOC_ENCODE(METHOD_DATA, buf, len, &method_data, &len, ret);
1332 free_METHOD_DATA(&method_data);
1335 e_data.length = len;
1336 e_text ="Need to use PA-ENC-TIMESTAMP/PA-PK-AS-REQ",
1338 ret = KRB5KDC_ERR_PREAUTH_REQUIRED;
1340 kdc_log(context, config, 0,
1341 "No preauth found, returning PREAUTH-REQUIRED -- %s",
1347 * Find the client key (for preauth ENC-TS verification and reply
1348 * encryption). Then the best encryption type for the KDC and
1349 * last the best session key that shared between the client and
1350 * KDC runtime enctypes.
1353 ret = _kdc_find_etype(context, client, b->etype.val, b->etype.len,
1356 kdc_log(context, config, 0,
1357 "Client (%s) has no support for etypes", client_name);
1361 ret = _kdc_get_preferred_key(context, config,
1362 server, server_name,
1368 * Select a session enctype from the list of the crypto systems
1369 * supported enctype, is supported by the client and is one of the
1370 * enctype of the enctype of the krbtgt.
1372 * The later is used as a hint what enctype all KDC are supporting
1373 * to make sure a newer version of KDC wont generate a session
1374 * enctype that and older version of a KDC in the same realm can't
1377 * But if the KDC admin is paranoid and doesn't want to have "no
1378 * the best" enctypes on the krbtgt, lets save the best pick from
1379 * the client list and hope that that will work for any other
1383 const krb5_enctype *p;
1384 krb5_enctype clientbest = ETYPE_NULL;
1387 p = krb5_kerberos_enctypes(context);
1389 sessionetype = ETYPE_NULL;
1391 for (i = 0; p[i] != ETYPE_NULL && sessionetype == ETYPE_NULL; i++) {
1392 if (krb5_enctype_valid(context, p[i]) != 0)
1395 for (j = 0; j < b->etype.len && sessionetype == ETYPE_NULL; j++) {
1397 /* check with client */
1398 if (p[i] != b->etype.val[j])
1400 /* save best of union of { client, crypto system } */
1401 if (clientbest == ETYPE_NULL)
1403 /* check with krbtgt */
1404 ret = hdb_enctype2key(context, &server->entry, p[i], &dummy);
1407 sessionetype = p[i];
1410 /* if krbtgt had no shared keys with client, pick clients best */
1411 if (clientbest != ETYPE_NULL && sessionetype == ETYPE_NULL) {
1412 sessionetype = clientbest;
1413 } else if (sessionetype == ETYPE_NULL) {
1414 kdc_log(context, config, 0,
1415 "Client (%s) from %s has no common enctypes with KDC"
1416 "to use for the session key",
1422 log_as_req(context, config, cetype, setype, b);
1424 if(f.renew || f.validate || f.proxy || f.forwarded || f.enc_tkt_in_skey
1425 || (f.request_anonymous && !config->allow_anonymous)) {
1426 ret = KRB5KDC_ERR_BADOPTION;
1427 kdc_log(context, config, 0, "Bad KDC options -- %s", client_name);
1432 rep.msg_type = krb_as_rep;
1433 copy_Realm(&client->entry.principal->realm, &rep.crealm);
1434 if (f.request_anonymous)
1435 _kdc_make_anonymous_principalname (&rep.cname);
1437 _krb5_principal2principalname(&rep.cname,
1438 client->entry.principal);
1439 rep.ticket.tkt_vno = 5;
1440 copy_Realm(&server->entry.principal->realm, &rep.ticket.realm);
1441 _krb5_principal2principalname(&rep.ticket.sname,
1442 server->entry.principal);
1443 /* java 1.6 expects the name to be the same type, lets allow that
1444 * uncomplicated name-types. */
1445 #define CNT(sp,t) (((sp)->sname->name_type) == KRB5_NT_##t)
1446 if (CNT(b, UNKNOWN) || CNT(b, PRINCIPAL) || CNT(b, SRV_INST) || CNT(b, SRV_HST) || CNT(b, SRV_XHST))
1447 rep.ticket.sname.name_type = b->sname->name_type;
1450 et.flags.initial = 1;
1451 if(client->entry.flags.forwardable && server->entry.flags.forwardable)
1452 et.flags.forwardable = f.forwardable;
1453 else if (f.forwardable) {
1454 ret = KRB5KDC_ERR_POLICY;
1455 kdc_log(context, config, 0,
1456 "Ticket may not be forwardable -- %s", client_name);
1459 if(client->entry.flags.proxiable && server->entry.flags.proxiable)
1460 et.flags.proxiable = f.proxiable;
1461 else if (f.proxiable) {
1462 ret = KRB5KDC_ERR_POLICY;
1463 kdc_log(context, config, 0,
1464 "Ticket may not be proxiable -- %s", client_name);
1467 if(client->entry.flags.postdate && server->entry.flags.postdate)
1468 et.flags.may_postdate = f.allow_postdate;
1469 else if (f.allow_postdate){
1470 ret = KRB5KDC_ERR_POLICY;
1471 kdc_log(context, config, 0,
1472 "Ticket may not be postdatable -- %s", client_name);
1476 /* check for valid set of addresses */
1477 if(!_kdc_check_addresses(context, config, b->addresses, from_addr)) {
1478 ret = KRB5KRB_AP_ERR_BADADDR;
1479 kdc_log(context, config, 0,
1480 "Bad address list requested -- %s", client_name);
1484 ret = krb5_generate_random_keyblock(context, sessionetype, &et.key);
1487 copy_PrincipalName(&rep.cname, &et.cname);
1488 copy_Realm(&rep.crealm, &et.crealm);
1494 start = et.authtime = kdc_time;
1496 if(f.postdated && req->req_body.from){
1497 ALLOC(et.starttime);
1498 start = *et.starttime = *req->req_body.from;
1499 et.flags.invalid = 1;
1500 et.flags.postdated = 1; /* XXX ??? */
1502 _kdc_fix_time(&b->till);
1505 /* be careful not overflowing */
1507 if(client->entry.max_life)
1508 t = start + min(t - start, *client->entry.max_life);
1509 if(server->entry.max_life)
1510 t = start + min(t - start, *server->entry.max_life);
1512 t = min(t, start + realm->max_life);
1515 if(f.renewable_ok && et.endtime < *b->till){
1517 if(b->rtime == NULL){
1521 if(*b->rtime < *b->till)
1522 *b->rtime = *b->till;
1524 if(f.renewable && b->rtime){
1528 if(client->entry.max_renew)
1529 t = start + min(t - start, *client->entry.max_renew);
1530 if(server->entry.max_renew)
1531 t = start + min(t - start, *server->entry.max_renew);
1533 t = min(t, start + realm->max_renew);
1535 ALLOC(et.renew_till);
1537 et.flags.renewable = 1;
1541 if (f.request_anonymous)
1542 et.flags.anonymous = 1;
1546 copy_HostAddresses(b->addresses, et.caddr);
1549 et.transited.tr_type = DOMAIN_X500_COMPRESS;
1550 krb5_data_zero(&et.transited.contents);
1552 copy_EncryptionKey(&et.key, &ek.key);
1554 /* The MIT ASN.1 library (obviously) doesn't tell lengths encoded
1555 * as 0 and as 0x80 (meaning indefinite length) apart, and is thus
1556 * incapable of correctly decoding SEQUENCE OF's of zero length.
1558 * To fix this, always send at least one no-op last_req
1560 * If there's a pw_end or valid_end we will use that,
1561 * otherwise just a dummy lr.
1563 ek.last_req.val = malloc(2 * sizeof(*ek.last_req.val));
1564 if (ek.last_req.val == NULL) {
1568 ek.last_req.len = 0;
1569 if (client->entry.pw_end
1570 && (config->kdc_warn_pwexpire == 0
1571 || kdc_time + config->kdc_warn_pwexpire >= *client->entry.pw_end)) {
1572 ek.last_req.val[ek.last_req.len].lr_type = LR_PW_EXPTIME;
1573 ek.last_req.val[ek.last_req.len].lr_value = *client->entry.pw_end;
1576 if (client->entry.valid_end) {
1577 ek.last_req.val[ek.last_req.len].lr_type = LR_ACCT_EXPTIME;
1578 ek.last_req.val[ek.last_req.len].lr_value = *client->entry.valid_end;
1581 if (ek.last_req.len == 0) {
1582 ek.last_req.val[ek.last_req.len].lr_type = LR_NONE;
1583 ek.last_req.val[ek.last_req.len].lr_value = 0;
1586 ek.nonce = b->nonce;
1587 if (client->entry.valid_end || client->entry.pw_end) {
1588 ALLOC(ek.key_expiration);
1589 if (client->entry.valid_end) {
1590 if (client->entry.pw_end)
1591 *ek.key_expiration = min(*client->entry.valid_end,
1592 *client->entry.pw_end);
1594 *ek.key_expiration = *client->entry.valid_end;
1596 *ek.key_expiration = *client->entry.pw_end;
1598 ek.key_expiration = NULL;
1599 ek.flags = et.flags;
1600 ek.authtime = et.authtime;
1602 ALLOC(ek.starttime);
1603 *ek.starttime = *et.starttime;
1605 ek.endtime = et.endtime;
1606 if (et.renew_till) {
1607 ALLOC(ek.renew_till);
1608 *ek.renew_till = *et.renew_till;
1610 copy_Realm(&rep.ticket.realm, &ek.srealm);
1611 copy_PrincipalName(&rep.ticket.sname, &ek.sname);
1614 copy_HostAddresses(et.caddr, ek.caddr);
1618 rep.padata->len = 0;
1619 rep.padata->val = NULL;
1621 reply_key = &ckey->key;
1624 ret = _kdc_pk_mk_pa_reply(context, config, pkp, client,
1626 &reply_key, rep.padata);
1629 ret = _kdc_add_inital_verified_cas(context,
1638 set_salt_padata (rep.padata, ckey->salt);
1640 /* Add signing of alias referral */
1641 if (f.canonicalize) {
1642 PA_ClientCanonicalized canon;
1648 memset(&canon, 0, sizeof(canon));
1650 canon.names.requested_name = *b->cname;
1651 canon.names.real_name = client->entry.principal->name;
1653 ASN1_MALLOC_ENCODE(PA_ClientCanonicalizedNames, data.data, data.length,
1654 &canon.names, &len, ret);
1657 if (data.length != len)
1658 krb5_abortx(context, "internal asn.1 error");
1660 /* sign using "returned session key" */
1661 ret = krb5_crypto_init(context, &et.key, 0, &crypto);
1667 ret = krb5_create_checksum(context, crypto,
1668 KRB5_KU_CANONICALIZED_NAMES, 0,
1669 data.data, data.length,
1670 &canon.canon_checksum);
1672 krb5_crypto_destroy(context, crypto);
1676 ASN1_MALLOC_ENCODE(PA_ClientCanonicalized, data.data, data.length,
1678 free_Checksum(&canon.canon_checksum);
1681 if (data.length != len)
1682 krb5_abortx(context, "internal asn.1 error");
1684 pa.padata_type = KRB5_PADATA_CLIENT_CANONICALIZED;
1685 pa.padata_value = data;
1686 ret = add_METHOD_DATA(rep.padata, &pa);
1692 if (rep.padata->len == 0) {
1698 if (send_pac_p(context, req)) {
1702 ret = _kdc_pac_generate(context, client, &p);
1704 kdc_log(context, config, 0, "PAC generation failed for -- %s",
1709 ret = _krb5_pac_sign(context, p, et.authtime,
1710 client->entry.principal,
1711 &skey->key, /* Server key */
1712 &skey->key, /* FIXME: should be krbtgt key */
1714 krb5_pac_free(context, p);
1716 kdc_log(context, config, 0, "PAC signing failed for -- %s",
1721 ret = _kdc_tkt_add_if_relevant_ad(context, &et,
1722 KRB5_AUTHDATA_WIN2K_PAC,
1724 krb5_data_free(&data);
1730 _kdc_log_timestamp(context, config, "AS-REQ", et.authtime, et.starttime,
1731 et.endtime, et.renew_till);
1733 /* do this as the last thing since this signs the EncTicketPart */
1734 ret = _kdc_add_KRB5SignedPath(context,
1744 ret = _kdc_encode_reply(context, config,
1745 &rep, &et, &ek, setype, server->entry.kvno,
1746 &skey->key, client->entry.kvno,
1747 reply_key, &e_text, reply);
1748 free_EncTicketPart(&et);
1749 free_EncKDCRepPart(&ek);
1754 if (datagram_reply && reply->length > config->max_datagram_reply_length) {
1755 krb5_data_free(reply);
1756 ret = KRB5KRB_ERR_RESPONSE_TOO_BIG;
1757 e_text = "Reply packet too large";
1763 krb5_mk_error(context,
1766 (e_data.data ? &e_data : NULL),
1776 _kdc_pk_free_client_param(context, pkp);
1781 krb5_free_principal(context, client_princ);
1784 krb5_free_principal(context, server_princ);
1787 _kdc_free_ent(context, client);
1789 _kdc_free_ent(context, server);
1794 * Add the AuthorizationData `data´ of `type´ to the last element in
1795 * the sequence of authorization_data in `tkt´ wrapped in an IF_RELEVANT
1799 _kdc_tkt_add_if_relevant_ad(krb5_context context,
1802 const krb5_data *data)
1804 krb5_error_code ret;
1807 if (tkt->authorization_data == NULL) {
1808 tkt->authorization_data = calloc(1, sizeof(*tkt->authorization_data));
1809 if (tkt->authorization_data == NULL) {
1810 krb5_set_error_string(context, "out of memory");
1815 /* add the entry to the last element */
1817 AuthorizationData ad = { 0, NULL };
1818 AuthorizationDataElement ade;
1821 ade.ad_data = *data;
1823 ret = add_AuthorizationData(&ad, &ade);
1825 krb5_set_error_string(context, "add AuthorizationData failed");
1829 ade.ad_type = KRB5_AUTHDATA_IF_RELEVANT;
1831 ASN1_MALLOC_ENCODE(AuthorizationData,
1832 ade.ad_data.data, ade.ad_data.length,
1834 free_AuthorizationData(&ad);
1836 krb5_set_error_string(context, "ASN.1 encode of "
1837 "AuthorizationData failed");
1840 if (ade.ad_data.length != size)
1841 krb5_abortx(context, "internal asn.1 encoder error");
1843 ret = add_AuthorizationData(tkt->authorization_data, &ade);
1844 der_free_octet_string(&ade.ad_data);
1846 krb5_set_error_string(context, "add AuthorizationData failed");