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 21529 2007-07-13 12:37:14Z 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:
372 only_older_enctype_p(const KDC_REQ *req)
376 for(i = 0; i < req->req_body.etype.len; i++) {
377 if (!older_enctype(req->req_body.etype.val[i]))
387 static krb5_error_code
388 make_etype_info_entry(krb5_context context, ETYPE_INFO_ENTRY *ent, Key *key)
390 ent->etype = key->key.keytype;
393 ALLOC(ent->salttype);
395 if(key->salt->type == hdb_pw_salt)
396 *ent->salttype = 0; /* or 1? or NULL? */
397 else if(key->salt->type == hdb_afs3_salt)
400 kdc_log(context, config, 0, "unknown salt-type: %d",
402 return KRB5KRB_ERR_GENERIC;
404 /* according to `the specs', we can't send a salt if
405 we have AFS3 salted key, but that requires that you
406 *know* what cell you are using (e.g by assuming
407 that the cell is the same as the realm in lower
410 ALLOC(ent->salttype);
411 *ent->salttype = key->salt->type;
414 * We shouldn't sent salttype since its incompatible with the
415 * specification and its break windows clients. The afs
416 * salting problem is solved by using KRB5-PADATA-AFS3-SALT
417 * implemented in Heimdal 0.7 and later.
419 ent->salttype = NULL;
421 krb5_copy_data(context, &key->salt->salt,
424 /* we return no salt type at all, as that should indicate
425 * the default salt type and make everybody happy. some
426 * systems (like w2k) dislike being told the salt type
429 ent->salttype = NULL;
435 static krb5_error_code
436 get_pa_etype_info(krb5_context context,
437 krb5_kdc_configuration *config,
438 METHOD_DATA *md, hdb_entry *client,
439 ENCTYPE *etypes, unsigned int etypes_len)
441 krb5_error_code ret = 0;
449 pa.len = client->keys.len;
450 if(pa.len > UINT_MAX/sizeof(*pa.val))
452 pa.val = malloc(pa.len * sizeof(*pa.val));
455 memset(pa.val, 0, pa.len * sizeof(*pa.val));
457 for(i = 0; i < client->keys.len; i++) {
458 for (j = 0; j < n; j++)
459 if (pa.val[j].etype == client->keys.val[i].key.keytype)
461 for(j = 0; j < etypes_len; j++) {
462 if(client->keys.val[i].key.keytype == etypes[j]) {
463 if (krb5_enctype_valid(context, etypes[j]) != 0)
465 if (!older_enctype(etypes[j]))
468 krb5_abortx(context, "internal error: n >= p.len");
469 if((ret = make_etype_info_entry(context,
471 &client->keys.val[i])) != 0) {
472 free_ETYPE_INFO(&pa);
479 for(i = 0; i < client->keys.len; i++) {
480 for(j = 0; j < etypes_len; j++) {
481 if(client->keys.val[i].key.keytype == etypes[j])
484 if (krb5_enctype_valid(context, client->keys.val[i].key.keytype) != 0)
486 if (!older_enctype(etypes[j]))
489 krb5_abortx(context, "internal error: n >= p.len");
490 if((ret = make_etype_info_entry(context,
492 &client->keys.val[i])) != 0) {
493 free_ETYPE_INFO(&pa);
500 /* stripped out newer enctypes */
504 ASN1_MALLOC_ENCODE(ETYPE_INFO, buf, len, &pa, &len, ret);
505 free_ETYPE_INFO(&pa);
508 ret = realloc_method_data(md);
513 md->val[md->len - 1].padata_type = KRB5_PADATA_ETYPE_INFO;
514 md->val[md->len - 1].padata_value.length = len;
515 md->val[md->len - 1].padata_value.data = buf;
523 extern int _krb5_AES_string_to_default_iterator;
525 static krb5_error_code
526 make_etype_info2_entry(ETYPE_INFO2_ENTRY *ent, Key *key)
528 ent->etype = key->key.keytype;
531 if (ent->salt == NULL)
533 *ent->salt = malloc(key->salt->salt.length + 1);
534 if (*ent->salt == NULL) {
539 memcpy(*ent->salt, key->salt->salt.data, key->salt->salt.length);
540 (*ent->salt)[key->salt->salt.length] = '\0';
544 ent->s2kparams = NULL;
546 switch (key->key.keytype) {
547 case ETYPE_AES128_CTS_HMAC_SHA1_96:
548 case ETYPE_AES256_CTS_HMAC_SHA1_96:
549 ALLOC(ent->s2kparams);
550 if (ent->s2kparams == NULL)
552 ent->s2kparams->length = 4;
553 ent->s2kparams->data = malloc(ent->s2kparams->length);
554 if (ent->s2kparams->data == NULL) {
555 free(ent->s2kparams);
556 ent->s2kparams = NULL;
559 _krb5_put_int(ent->s2kparams->data,
560 _krb5_AES_string_to_default_iterator,
561 ent->s2kparams->length);
563 case ETYPE_DES_CBC_CRC:
564 case ETYPE_DES_CBC_MD4:
565 case ETYPE_DES_CBC_MD5:
566 /* Check if this was a AFS3 salted key */
567 if(key->salt && key->salt->type == hdb_afs3_salt){
568 ALLOC(ent->s2kparams);
569 if (ent->s2kparams == NULL)
571 ent->s2kparams->length = 1;
572 ent->s2kparams->data = malloc(ent->s2kparams->length);
573 if (ent->s2kparams->data == NULL) {
574 free(ent->s2kparams);
575 ent->s2kparams = NULL;
578 _krb5_put_int(ent->s2kparams->data,
580 ent->s2kparams->length);
590 * Return an ETYPE-INFO2. Enctypes are storted the same way as in the
591 * database (client supported enctypes first, then the unsupported
595 static krb5_error_code
596 get_pa_etype_info2(krb5_context context,
597 krb5_kdc_configuration *config,
598 METHOD_DATA *md, hdb_entry *client,
599 ENCTYPE *etypes, unsigned int etypes_len)
601 krb5_error_code ret = 0;
608 pa.len = client->keys.len;
609 if(pa.len > UINT_MAX/sizeof(*pa.val))
611 pa.val = malloc(pa.len * sizeof(*pa.val));
614 memset(pa.val, 0, pa.len * sizeof(*pa.val));
616 for(i = 0; i < client->keys.len; i++) {
617 for (j = 0; j < n; j++)
618 if (pa.val[j].etype == client->keys.val[i].key.keytype)
620 for(j = 0; j < etypes_len; j++) {
621 if(client->keys.val[i].key.keytype == etypes[j]) {
622 if (krb5_enctype_valid(context, etypes[j]) != 0)
624 if((ret = make_etype_info2_entry(&pa.val[n++],
625 &client->keys.val[i])) != 0) {
626 free_ETYPE_INFO2(&pa);
633 /* send enctypes that the cliene doesn't know about too */
634 for(i = 0; i < client->keys.len; i++) {
635 for(j = 0; j < etypes_len; j++) {
636 if(client->keys.val[i].key.keytype == etypes[j])
639 if (krb5_enctype_valid(context, client->keys.val[i].key.keytype) != 0)
641 if((ret = make_etype_info2_entry(&pa.val[n++],
642 &client->keys.val[i])) != 0) {
643 free_ETYPE_INFO2(&pa);
651 ret = krb5_unparse_name(context, client->principal, &name);
653 name = rk_UNCONST("<unparse_name failed>");
654 kdc_log(context, config, 0,
655 "internal error in get_pa_etype_info2(%s): %d != %d",
662 ASN1_MALLOC_ENCODE(ETYPE_INFO2, buf, len, &pa, &len, ret);
663 free_ETYPE_INFO2(&pa);
666 ret = realloc_method_data(md);
671 md->val[md->len - 1].padata_type = KRB5_PADATA_ETYPE_INFO2;
672 md->val[md->len - 1].padata_value.length = len;
673 md->val[md->len - 1].padata_value.data = buf;
682 log_as_req(krb5_context context,
683 krb5_kdc_configuration *config,
686 const KDC_REQ_BODY *b)
689 struct rk_strpool *p = NULL;
693 for (i = 0; i < b->etype.len; i++) {
694 ret = krb5_enctype_to_string(context, b->etype.val[i], &str);
696 p = rk_strpoolprintf(p, "%s", str);
699 p = rk_strpoolprintf(p, "%d", b->etype.val[i]);
700 if (p && i + 1 < b->etype.len)
701 p = rk_strpoolprintf(p, ", ");
703 kdc_log(context, config, 0, "out of memory");
708 p = rk_strpoolprintf(p, "no encryption types");
710 str = rk_strpoolcollect(p);
711 kdc_log(context, config, 0, "Client supported enctypes: %s", str);
718 ret = krb5_enctype_to_string(context, cetype, &cet);
720 ret = krb5_enctype_to_string(context, setype, &set);
722 kdc_log(context, config, 5, "Using %s/%s", cet, set);
728 kdc_log(context, config, 5, "Using e-types %d/%d", cetype, setype);
733 unparse_flags(KDCOptions2int(b->kdc_options), asn1_KDCOptions_units(),
734 fixedstr, sizeof(fixedstr));
736 kdc_log(context, config, 2, "Requested flags: %s", fixedstr);
741 * verify the flags on `client' and `server', returning 0
742 * if they are OK and generating an error messages and returning
743 * and error code otherwise.
747 _kdc_check_flags(krb5_context context,
748 krb5_kdc_configuration *config,
749 hdb_entry_ex *client_ex, const char *client_name,
750 hdb_entry_ex *server_ex, const char *server_name,
751 krb5_boolean is_as_req)
753 if(client_ex != NULL) {
754 hdb_entry *client = &client_ex->entry;
757 if (client->flags.invalid) {
758 kdc_log(context, config, 0,
759 "Client (%s) has invalid bit set", client_name);
760 return KRB5KDC_ERR_POLICY;
763 if(!client->flags.client){
764 kdc_log(context, config, 0,
765 "Principal may not act as client -- %s", client_name);
766 return KRB5KDC_ERR_POLICY;
769 if (client->valid_start && *client->valid_start > kdc_time) {
770 char starttime_str[100];
771 krb5_format_time(context, *client->valid_start,
772 starttime_str, sizeof(starttime_str), TRUE);
773 kdc_log(context, config, 0,
774 "Client not yet valid until %s -- %s",
775 starttime_str, client_name);
776 return KRB5KDC_ERR_CLIENT_NOTYET;
779 if (client->valid_end && *client->valid_end < kdc_time) {
780 char endtime_str[100];
781 krb5_format_time(context, *client->valid_end,
782 endtime_str, sizeof(endtime_str), TRUE);
783 kdc_log(context, config, 0,
784 "Client expired at %s -- %s",
785 endtime_str, client_name);
786 return KRB5KDC_ERR_NAME_EXP;
789 if (client->pw_end && *client->pw_end < kdc_time
790 && (server_ex == NULL || !server_ex->entry.flags.change_pw)) {
792 krb5_format_time(context, *client->pw_end,
793 pwend_str, sizeof(pwend_str), TRUE);
794 kdc_log(context, config, 0,
795 "Client's key has expired at %s -- %s",
796 pwend_str, client_name);
797 return KRB5KDC_ERR_KEY_EXPIRED;
803 if (server_ex != NULL) {
804 hdb_entry *server = &server_ex->entry;
806 if (server->flags.invalid) {
807 kdc_log(context, config, 0,
808 "Server has invalid flag set -- %s", server_name);
809 return KRB5KDC_ERR_POLICY;
812 if(!server->flags.server){
813 kdc_log(context, config, 0,
814 "Principal may not act as server -- %s", server_name);
815 return KRB5KDC_ERR_POLICY;
818 if(!is_as_req && server->flags.initial) {
819 kdc_log(context, config, 0,
820 "AS-REQ is required for server -- %s", server_name);
821 return KRB5KDC_ERR_POLICY;
824 if (server->valid_start && *server->valid_start > kdc_time) {
825 char starttime_str[100];
826 krb5_format_time(context, *server->valid_start,
827 starttime_str, sizeof(starttime_str), TRUE);
828 kdc_log(context, config, 0,
829 "Server not yet valid until %s -- %s",
830 starttime_str, server_name);
831 return KRB5KDC_ERR_SERVICE_NOTYET;
834 if (server->valid_end && *server->valid_end < kdc_time) {
835 char endtime_str[100];
836 krb5_format_time(context, *server->valid_end,
837 endtime_str, sizeof(endtime_str), TRUE);
838 kdc_log(context, config, 0,
839 "Server expired at %s -- %s",
840 endtime_str, server_name);
841 return KRB5KDC_ERR_SERVICE_EXP;
844 if (server->pw_end && *server->pw_end < kdc_time) {
846 krb5_format_time(context, *server->pw_end,
847 pwend_str, sizeof(pwend_str), TRUE);
848 kdc_log(context, config, 0,
849 "Server's key has expired at -- %s",
850 pwend_str, server_name);
851 return KRB5KDC_ERR_KEY_EXPIRED;
858 * Return TRUE if `from' is part of `addresses' taking into consideration
859 * the configuration variables that tells us how strict we should be about
864 _kdc_check_addresses(krb5_context context,
865 krb5_kdc_configuration *config,
866 HostAddresses *addresses, const struct sockaddr *from)
871 krb5_boolean only_netbios = TRUE;
874 if(config->check_ticket_addresses == 0)
877 if(addresses == NULL)
878 return config->allow_null_ticket_addresses;
880 for (i = 0; i < addresses->len; ++i) {
881 if (addresses->val[i].addr_type != KRB5_ADDRESS_NETBIOS) {
882 only_netbios = FALSE;
886 /* Windows sends it's netbios name, which I can only assume is
887 * used for the 'allowed workstations' check. This is painful,
888 * but we still want to check IP addresses if they happen to be
893 return config->allow_null_ticket_addresses;
895 ret = krb5_sockaddr2address (context, from, &addr);
899 result = krb5_address_search(context, &addr, addresses);
900 krb5_free_address (context, &addr);
909 send_pac_p(krb5_context context, KDC_REQ *req)
912 PA_PAC_REQUEST pacreq;
916 pa = _kdc_find_padata(req, &i, KRB5_PADATA_PA_PAC_REQUEST);
920 ret = decode_PA_PAC_REQUEST(pa->padata_value.data,
921 pa->padata_value.length,
926 i = pacreq.include_pac;
927 free_PA_PAC_REQUEST(&pacreq);
938 _kdc_as_rep(krb5_context context,
939 krb5_kdc_configuration *config,
941 const krb5_data *req_buffer,
944 struct sockaddr *from_addr,
947 KDC_REQ_BODY *b = &req->req_body;
949 KDCOptions f = b->kdc_options;
950 hdb_entry_ex *client = NULL, *server = NULL;
951 krb5_enctype cetype, setype, sessionetype;
955 krb5_principal client_princ = NULL, server_princ = NULL;
956 char *client_name = NULL, *server_name = NULL;
957 krb5_error_code ret = 0;
958 const char *e_text = NULL;
961 EncryptionKey *reply_key;
964 pk_client_params *pkp = NULL;
967 memset(&rep, 0, sizeof(rep));
968 krb5_data_zero(&e_data);
971 flags |= HDB_F_CANON;
973 if(b->sname == NULL){
974 ret = KRB5KRB_ERR_GENERIC;
975 e_text = "No server in request";
977 ret = _krb5_principalname2krb5_principal (context,
982 ret = krb5_unparse_name(context, server_princ, &server_name);
985 kdc_log(context, config, 0,
986 "AS-REQ malformed server name from %s", from);
990 if(b->cname == NULL){
991 ret = KRB5KRB_ERR_GENERIC;
992 e_text = "No client in request";
995 if (b->cname->name_type == KRB5_NT_ENTERPRISE_PRINCIPAL) {
996 if (b->cname->name_string.len != 1) {
997 kdc_log(context, config, 0,
998 "AS-REQ malformed canon request from %s, "
999 "enterprise name with %d name components",
1000 from, b->cname->name_string.len);
1001 ret = KRB5_PARSE_MALFORMED;
1004 ret = krb5_parse_name(context, b->cname->name_string.val[0],
1009 ret = _krb5_principalname2krb5_principal (context,
1016 ret = krb5_unparse_name(context, client_princ, &client_name);
1019 kdc_log(context, config, 0,
1020 "AS-REQ malformed client name from %s", from);
1024 kdc_log(context, config, 0, "AS-REQ %s from %s for %s",
1025 client_name, from, server_name);
1027 ret = _kdc_db_fetch(context, config, client_princ,
1028 HDB_F_GET_CLIENT | flags, NULL, &client);
1030 kdc_log(context, config, 0, "UNKNOWN -- %s: %s", client_name,
1031 krb5_get_err_text(context, ret));
1032 ret = KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN;
1036 ret = _kdc_db_fetch(context, config, server_princ,
1037 HDB_F_GET_SERVER|HDB_F_GET_KRBTGT,
1040 kdc_log(context, config, 0, "UNKNOWN -- %s: %s", server_name,
1041 krb5_get_err_text(context, ret));
1042 ret = KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN;
1046 ret = _kdc_windc_client_access(context, client, req, &e_data);
1050 ret = _kdc_check_flags(context, config,
1051 client, client_name,
1052 server, server_name,
1057 memset(&et, 0, sizeof(et));
1058 memset(&ek, 0, sizeof(ek));
1065 log_patypes(context, config, req->padata);
1068 kdc_log(context, config, 5,
1069 "Looking for PKINIT pa-data -- %s", client_name);
1071 e_text = "No PKINIT PA found";
1074 if ((pa = _kdc_find_padata(req, &i, KRB5_PADATA_PK_AS_REQ)))
1078 if((pa = _kdc_find_padata(req, &i, KRB5_PADATA_PK_AS_REQ_WIN)))
1082 char *client_cert = NULL;
1084 ret = _kdc_pk_rd_padata(context, config, req, pa, &pkp);
1086 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
1087 kdc_log(context, config, 5,
1088 "Failed to decode PKINIT PA-DATA -- %s",
1092 if (ret == 0 && pkp == NULL)
1095 ret = _kdc_pk_check_client(context,
1101 e_text = "PKINIT certificate not allowed to "
1102 "impersonate principal";
1103 _kdc_pk_free_client_param(context, pkp);
1105 kdc_log(context, config, 0, "%s", e_text);
1110 et.flags.pre_authent = 1;
1111 kdc_log(context, config, 0,
1112 "PKINIT pre-authentication succeeded -- %s using %s",
1113 client_name, client_cert);
1120 kdc_log(context, config, 5, "Looking for ENC-TS pa-data -- %s",
1124 e_text = "No ENC-TS found";
1125 while((pa = _kdc_find_padata(req, &i, KRB5_PADATA_ENC_TIMESTAMP))){
1129 EncryptedData enc_data;
1135 ret = decode_EncryptedData(pa->padata_value.data,
1136 pa->padata_value.length,
1140 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
1141 kdc_log(context, config, 5, "Failed to decode PA-DATA -- %s",
1146 ret = hdb_enctype2key(context, &client->entry,
1147 enc_data.etype, &pa_key);
1150 e_text = "No key matches pa-data";
1151 ret = KRB5KDC_ERR_ETYPE_NOSUPP;
1152 if(krb5_enctype_to_string(context, enc_data.etype, &estr))
1155 kdc_log(context, config, 5,
1156 "No client key matching pa-data (%d) -- %s",
1157 enc_data.etype, client_name);
1159 kdc_log(context, config, 5,
1160 "No client key matching pa-data (%s) -- %s",
1164 free_EncryptedData(&enc_data);
1169 ret = krb5_crypto_init(context, &pa_key->key, 0, &crypto);
1171 kdc_log(context, config, 0, "krb5_crypto_init failed: %s",
1172 krb5_get_err_text(context, ret));
1173 free_EncryptedData(&enc_data);
1177 ret = krb5_decrypt_EncryptedData (context,
1179 KRB5_KU_PA_ENC_TIMESTAMP,
1182 krb5_crypto_destroy(context, crypto);
1184 krb5_error_code ret2;
1185 ret2 = krb5_enctype_to_string(context,
1186 pa_key->key.keytype, &str);
1189 kdc_log(context, config, 5,
1190 "Failed to decrypt PA-DATA -- %s "
1191 "(enctype %s) error %s",
1193 str ? str : "unknown enctype",
1194 krb5_get_err_text(context, ret));
1197 if(hdb_next_enctype2key(context, &client->entry,
1198 enc_data.etype, &pa_key) == 0)
1200 e_text = "Failed to decrypt PA-DATA";
1202 free_EncryptedData(&enc_data);
1203 ret = KRB5KDC_ERR_PREAUTH_FAILED;
1206 free_EncryptedData(&enc_data);
1207 ret = decode_PA_ENC_TS_ENC(ts_data.data,
1211 krb5_data_free(&ts_data);
1213 e_text = "Failed to decode PA-ENC-TS-ENC";
1214 ret = KRB5KDC_ERR_PREAUTH_FAILED;
1215 kdc_log(context, config,
1216 5, "Failed to decode PA-ENC-TS_ENC -- %s",
1220 free_PA_ENC_TS_ENC(&p);
1221 if (abs(kdc_time - p.patimestamp) > context->max_skew) {
1222 char client_time[100];
1224 krb5_format_time(context, p.patimestamp,
1225 client_time, sizeof(client_time), TRUE);
1227 ret = KRB5KRB_AP_ERR_SKEW;
1228 kdc_log(context, config, 0,
1229 "Too large time skew, "
1230 "client time %s is out by %u > %u seconds -- %s",
1232 (unsigned)abs(kdc_time - p.patimestamp),
1236 /* This code is from samba, needs testing */
1238 * the following is needed to make windows clients
1239 * to retry using the timestamp in the error message
1241 * this is maybe a bug in windows to not trying when e_text
1246 e_text = "Too large time skew";
1250 et.flags.pre_authent = 1;
1252 ret = krb5_enctype_to_string(context,pa_key->key.keytype, &str);
1256 kdc_log(context, config, 2,
1257 "ENC-TS Pre-authentication succeeded -- %s using %s",
1258 client_name, str ? str : "unknown enctype");
1265 if(found_pa == 0 && config->require_preauth)
1267 /* We come here if we found a pa-enc-timestamp, but if there
1268 was some problem with it, other than too large skew */
1269 if(found_pa && et.flags.pre_authent == 0){
1270 kdc_log(context, config, 0, "%s -- %s", e_text, client_name);
1274 }else if (config->require_preauth
1275 || client->entry.flags.require_preauth
1276 || server->entry.flags.require_preauth) {
1277 METHOD_DATA method_data;
1283 method_data.len = 0;
1284 method_data.val = NULL;
1286 ret = realloc_method_data(&method_data);
1287 pa = &method_data.val[method_data.len-1];
1288 pa->padata_type = KRB5_PADATA_ENC_TIMESTAMP;
1289 pa->padata_value.length = 0;
1290 pa->padata_value.data = NULL;
1293 ret = realloc_method_data(&method_data);
1294 pa = &method_data.val[method_data.len-1];
1295 pa->padata_type = KRB5_PADATA_PK_AS_REQ;
1296 pa->padata_value.length = 0;
1297 pa->padata_value.data = NULL;
1299 ret = realloc_method_data(&method_data);
1300 pa = &method_data.val[method_data.len-1];
1301 pa->padata_type = KRB5_PADATA_PK_AS_REQ_WIN;
1302 pa->padata_value.length = 0;
1303 pa->padata_value.data = NULL;
1308 * - If the client only knows about old enctypes, then send
1309 * both info replies (we send 'info' first in the list).
1310 * - If the client is 'modern', because it knows about 'new'
1311 * enctype types, then only send the 'info2' reply.
1315 if (only_older_enctype_p(req))
1316 ret = get_pa_etype_info(context, config,
1317 &method_data, &client->entry,
1318 b->etype.val, b->etype.len);
1320 ret = get_pa_etype_info2(context, config, &method_data,
1321 &client->entry, b->etype.val, b->etype.len);
1324 ASN1_MALLOC_ENCODE(METHOD_DATA, buf, len, &method_data, &len, ret);
1325 free_METHOD_DATA(&method_data);
1328 e_data.length = len;
1329 e_text ="Need to use PA-ENC-TIMESTAMP/PA-PK-AS-REQ",
1331 ret = KRB5KDC_ERR_PREAUTH_REQUIRED;
1333 kdc_log(context, config, 0,
1334 "No preauth found, returning PREAUTH-REQUIRED -- %s",
1340 * Find the client key (for preauth ENC-TS verification and reply
1341 * encryption). Then the best encryption type for the KDC and
1342 * last the best session key that shared between the client and
1343 * KDC runtime enctypes.
1346 ret = _kdc_find_etype(context, client, b->etype.val, b->etype.len,
1349 kdc_log(context, config, 0,
1350 "Client (%s) has no support for etypes", client_name);
1354 ret = _kdc_get_preferred_key(context, config,
1355 server, server_name,
1361 * Select a session enctype from the list of the crypto systems
1362 * supported enctype, is supported by the client and is one of the
1363 * enctype of the enctype of the krbtgt.
1365 * The later is used as a hint what enctype all KDC are supporting
1366 * to make sure a newer version of KDC wont generate a session
1367 * enctype that and older version of a KDC in the same realm can't
1370 * But if the KDC admin is paranoid and doesn't want to have "no
1371 * the best" enctypes on the krbtgt, lets save the best pick from
1372 * the client list and hope that that will work for any other
1376 const krb5_enctype *p;
1377 krb5_enctype clientbest = ETYPE_NULL;
1380 p = krb5_kerberos_enctypes(context);
1382 sessionetype = ETYPE_NULL;
1384 for (i = 0; p[i] != ETYPE_NULL && sessionetype == ETYPE_NULL; i++) {
1385 if (krb5_enctype_valid(context, p[i]) != 0)
1388 for (j = 0; j < b->etype.len && sessionetype == ETYPE_NULL; j++) {
1390 /* check with client */
1391 if (p[i] != b->etype.val[j])
1393 /* save best of union of { client, crypto system } */
1394 if (clientbest == ETYPE_NULL)
1396 /* check with krbtgt */
1397 ret = hdb_enctype2key(context, &server->entry, p[i], &dummy);
1400 sessionetype = p[i];
1403 /* if krbtgt had no shared keys with client, pick clients best */
1404 if (clientbest != ETYPE_NULL && sessionetype == ETYPE_NULL) {
1405 sessionetype = clientbest;
1406 } else if (sessionetype == ETYPE_NULL) {
1407 kdc_log(context, config, 0,
1408 "Client (%s) from %s has no common enctypes with KDC"
1409 "to use for the session key",
1415 log_as_req(context, config, cetype, setype, b);
1417 if(f.renew || f.validate || f.proxy || f.forwarded || f.enc_tkt_in_skey
1418 || (f.request_anonymous && !config->allow_anonymous)) {
1419 ret = KRB5KDC_ERR_BADOPTION;
1420 kdc_log(context, config, 0, "Bad KDC options -- %s", client_name);
1425 rep.msg_type = krb_as_rep;
1426 copy_Realm(&client->entry.principal->realm, &rep.crealm);
1427 if (f.request_anonymous)
1428 _kdc_make_anonymous_principalname (&rep.cname);
1430 _krb5_principal2principalname(&rep.cname,
1431 client->entry.principal);
1432 rep.ticket.tkt_vno = 5;
1433 copy_Realm(&server->entry.principal->realm, &rep.ticket.realm);
1434 _krb5_principal2principalname(&rep.ticket.sname,
1435 server->entry.principal);
1436 /* java 1.6 expects the name to be the same type, lets allow that
1437 * uncomplicated name-types. */
1438 #define CNT(sp,t) (((sp)->sname->name_type) == KRB5_NT_##t)
1439 if (CNT(b, UNKNOWN) || CNT(b, PRINCIPAL) || CNT(b, SRV_INST) || CNT(b, SRV_HST) || CNT(b, SRV_XHST))
1440 rep.ticket.sname.name_type = b->sname->name_type;
1443 et.flags.initial = 1;
1444 if(client->entry.flags.forwardable && server->entry.flags.forwardable)
1445 et.flags.forwardable = f.forwardable;
1446 else if (f.forwardable) {
1447 ret = KRB5KDC_ERR_POLICY;
1448 kdc_log(context, config, 0,
1449 "Ticket may not be forwardable -- %s", client_name);
1452 if(client->entry.flags.proxiable && server->entry.flags.proxiable)
1453 et.flags.proxiable = f.proxiable;
1454 else if (f.proxiable) {
1455 ret = KRB5KDC_ERR_POLICY;
1456 kdc_log(context, config, 0,
1457 "Ticket may not be proxiable -- %s", client_name);
1460 if(client->entry.flags.postdate && server->entry.flags.postdate)
1461 et.flags.may_postdate = f.allow_postdate;
1462 else if (f.allow_postdate){
1463 ret = KRB5KDC_ERR_POLICY;
1464 kdc_log(context, config, 0,
1465 "Ticket may not be postdatable -- %s", client_name);
1469 /* check for valid set of addresses */
1470 if(!_kdc_check_addresses(context, config, b->addresses, from_addr)) {
1471 ret = KRB5KRB_AP_ERR_BADADDR;
1472 kdc_log(context, config, 0,
1473 "Bad address list requested -- %s", client_name);
1477 ret = krb5_generate_random_keyblock(context, sessionetype, &et.key);
1480 copy_PrincipalName(&rep.cname, &et.cname);
1481 copy_Realm(&rep.crealm, &et.crealm);
1487 start = et.authtime = kdc_time;
1489 if(f.postdated && req->req_body.from){
1490 ALLOC(et.starttime);
1491 start = *et.starttime = *req->req_body.from;
1492 et.flags.invalid = 1;
1493 et.flags.postdated = 1; /* XXX ??? */
1495 _kdc_fix_time(&b->till);
1498 /* be careful not overflowing */
1500 if(client->entry.max_life)
1501 t = start + min(t - start, *client->entry.max_life);
1502 if(server->entry.max_life)
1503 t = start + min(t - start, *server->entry.max_life);
1505 t = min(t, start + realm->max_life);
1508 if(f.renewable_ok && et.endtime < *b->till){
1510 if(b->rtime == NULL){
1514 if(*b->rtime < *b->till)
1515 *b->rtime = *b->till;
1517 if(f.renewable && b->rtime){
1521 if(client->entry.max_renew)
1522 t = start + min(t - start, *client->entry.max_renew);
1523 if(server->entry.max_renew)
1524 t = start + min(t - start, *server->entry.max_renew);
1526 t = min(t, start + realm->max_renew);
1528 ALLOC(et.renew_till);
1530 et.flags.renewable = 1;
1534 if (f.request_anonymous)
1535 et.flags.anonymous = 1;
1539 copy_HostAddresses(b->addresses, et.caddr);
1542 et.transited.tr_type = DOMAIN_X500_COMPRESS;
1543 krb5_data_zero(&et.transited.contents);
1545 copy_EncryptionKey(&et.key, &ek.key);
1547 /* The MIT ASN.1 library (obviously) doesn't tell lengths encoded
1548 * as 0 and as 0x80 (meaning indefinite length) apart, and is thus
1549 * incapable of correctly decoding SEQUENCE OF's of zero length.
1551 * To fix this, always send at least one no-op last_req
1553 * If there's a pw_end or valid_end we will use that,
1554 * otherwise just a dummy lr.
1556 ek.last_req.val = malloc(2 * sizeof(*ek.last_req.val));
1557 ek.last_req.len = 0;
1558 if (client->entry.pw_end
1559 && (config->kdc_warn_pwexpire == 0
1560 || kdc_time + config->kdc_warn_pwexpire >= *client->entry.pw_end)) {
1561 ek.last_req.val[ek.last_req.len].lr_type = LR_PW_EXPTIME;
1562 ek.last_req.val[ek.last_req.len].lr_value = *client->entry.pw_end;
1565 if (client->entry.valid_end) {
1566 ek.last_req.val[ek.last_req.len].lr_type = LR_ACCT_EXPTIME;
1567 ek.last_req.val[ek.last_req.len].lr_value = *client->entry.valid_end;
1570 if (ek.last_req.len == 0) {
1571 ek.last_req.val[ek.last_req.len].lr_type = LR_NONE;
1572 ek.last_req.val[ek.last_req.len].lr_value = 0;
1575 ek.nonce = b->nonce;
1576 if (client->entry.valid_end || client->entry.pw_end) {
1577 ALLOC(ek.key_expiration);
1578 if (client->entry.valid_end) {
1579 if (client->entry.pw_end)
1580 *ek.key_expiration = min(*client->entry.valid_end,
1581 *client->entry.pw_end);
1583 *ek.key_expiration = *client->entry.valid_end;
1585 *ek.key_expiration = *client->entry.pw_end;
1587 ek.key_expiration = NULL;
1588 ek.flags = et.flags;
1589 ek.authtime = et.authtime;
1591 ALLOC(ek.starttime);
1592 *ek.starttime = *et.starttime;
1594 ek.endtime = et.endtime;
1595 if (et.renew_till) {
1596 ALLOC(ek.renew_till);
1597 *ek.renew_till = *et.renew_till;
1599 copy_Realm(&rep.ticket.realm, &ek.srealm);
1600 copy_PrincipalName(&rep.ticket.sname, &ek.sname);
1603 copy_HostAddresses(et.caddr, ek.caddr);
1607 rep.padata->len = 0;
1608 rep.padata->val = NULL;
1610 reply_key = &ckey->key;
1613 ret = _kdc_pk_mk_pa_reply(context, config, pkp, client,
1615 &reply_key, rep.padata);
1618 ret = _kdc_add_inital_verified_cas(context,
1627 set_salt_padata (rep.padata, ckey->salt);
1629 /* Add signing of alias referral */
1630 if (f.canonicalize) {
1631 PA_ClientCanonicalized canon;
1637 memset(&canon, 0, sizeof(canon));
1639 canon.names.requested_name = *b->cname;
1640 canon.names.real_name = client->entry.principal->name;
1642 ASN1_MALLOC_ENCODE(PA_ClientCanonicalizedNames, data.data, data.length,
1643 &canon.names, &len, ret);
1646 if (data.length != len)
1647 krb5_abortx(context, "internal asn.1 error");
1649 /* sign using "returned session key" */
1650 ret = krb5_crypto_init(context, &et.key, 0, &crypto);
1656 ret = krb5_create_checksum(context, crypto,
1657 KRB5_KU_CANONICALIZED_NAMES, 0,
1658 data.data, data.length,
1659 &canon.canon_checksum);
1661 krb5_crypto_destroy(context, crypto);
1665 ASN1_MALLOC_ENCODE(PA_ClientCanonicalized, data.data, data.length,
1667 free_Checksum(&canon.canon_checksum);
1670 if (data.length != len)
1671 krb5_abortx(context, "internal asn.1 error");
1673 pa.padata_type = KRB5_PADATA_CLIENT_CANONICALIZED;
1674 pa.padata_value = data;
1675 ret = add_METHOD_DATA(rep.padata, &pa);
1681 if (rep.padata->len == 0) {
1687 if (send_pac_p(context, req)) {
1691 ret = _kdc_pac_generate(context, client, &p);
1693 kdc_log(context, config, 0, "PAC generation failed for -- %s",
1698 ret = _krb5_pac_sign(context, p, et.authtime,
1699 client->entry.principal,
1700 &skey->key, /* Server key */
1701 &skey->key, /* FIXME: should be krbtgt key */
1703 krb5_pac_free(context, p);
1705 kdc_log(context, config, 0, "PAC signing failed for -- %s",
1710 ret = _kdc_tkt_add_if_relevant_ad(context, &et,
1711 KRB5_AUTHDATA_WIN2K_PAC,
1713 krb5_data_free(&data);
1719 _kdc_log_timestamp(context, config, "AS-REQ", et.authtime, et.starttime,
1720 et.endtime, et.renew_till);
1722 /* do this as the last thing since this signs the EncTicketPart */
1723 ret = _kdc_add_KRB5SignedPath(context,
1733 ret = _kdc_encode_reply(context, config,
1734 &rep, &et, &ek, setype, server->entry.kvno,
1735 &skey->key, client->entry.kvno,
1736 reply_key, &e_text, reply);
1737 free_EncTicketPart(&et);
1738 free_EncKDCRepPart(&ek);
1743 if (datagram_reply && reply->length > config->max_datagram_reply_length) {
1744 krb5_data_free(reply);
1745 ret = KRB5KRB_ERR_RESPONSE_TOO_BIG;
1746 e_text = "Reply packet too large";
1752 krb5_mk_error(context,
1755 (e_data.data ? &e_data : NULL),
1765 _kdc_pk_free_client_param(context, pkp);
1770 krb5_free_principal(context, client_princ);
1773 krb5_free_principal(context, server_princ);
1776 _kdc_free_ent(context, client);
1778 _kdc_free_ent(context, server);
1783 * Add the AuthorizationData `data´ of `type´ to the last element in
1784 * the sequence of authorization_data in `tkt´ wrapped in an IF_RELEVANT
1788 _kdc_tkt_add_if_relevant_ad(krb5_context context,
1791 const krb5_data *data)
1793 krb5_error_code ret;
1796 if (tkt->authorization_data == NULL) {
1797 tkt->authorization_data = calloc(1, sizeof(*tkt->authorization_data));
1798 if (tkt->authorization_data == NULL) {
1799 krb5_set_error_string(context, "out of memory");
1804 /* add the entry to the last element */
1806 AuthorizationData ad = { 0, NULL };
1807 AuthorizationDataElement ade;
1810 ade.ad_data = *data;
1812 ret = add_AuthorizationData(&ad, &ade);
1814 krb5_set_error_string(context, "add AuthorizationData failed");
1818 ade.ad_type = KRB5_AUTHDATA_IF_RELEVANT;
1820 ASN1_MALLOC_ENCODE(AuthorizationData,
1821 ade.ad_data.data, ade.ad_data.length,
1823 free_AuthorizationData(&ad);
1825 krb5_set_error_string(context, "ASN.1 encode of "
1826 "AuthorizationData failed");
1829 if (ade.ad_data.length != size)
1830 krb5_abortx(context, "internal asn.1 encoder error");
1832 ret = add_AuthorizationData(tkt->authorization_data, &ade);
1833 der_free_octet_string(&ade.ad_data);
1835 krb5_set_error_string(context, "add AuthorizationData failed");