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
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 * This is a hack to allow predefined weak services, like afs to
89 * still use weak types
93 _kdc_is_weak_exception(krb5_principal principal, krb5_enctype etype)
95 if (principal->name.name_string.len > 0 &&
96 strcmp(principal->name.name_string.val[0], "afs") == 0 &&
97 (etype == ETYPE_DES_CBC_CRC
98 || etype == ETYPE_DES_CBC_MD4
99 || etype == ETYPE_DES_CBC_MD5))
106 * Detect if `key' is the using the the precomputed `default_salt'.
110 is_default_salt_p(const krb5_salt *default_salt, const Key *key)
112 if (key->salt == NULL)
114 if (default_salt->salttype != key->salt->type)
116 if (krb5_data_cmp(&default_salt->saltvalue, &key->salt->salt))
122 * return the first appropriate key of `princ' in `ret_key'. Look for
123 * all the etypes in (`etypes', `len'), stopping as soon as we find
124 * one, but preferring one that has default salt
128 _kdc_find_etype(krb5_context context, const hdb_entry_ex *princ,
129 krb5_enctype *etypes, unsigned len,
130 Key **ret_key, krb5_enctype *ret_etype)
133 krb5_error_code ret = KRB5KDC_ERR_ETYPE_NOSUPP;
136 krb5_get_pw_salt (context, princ->entry.principal, &def_salt);
138 for(i = 0; ret != 0 && i < len ; i++) {
141 if (krb5_enctype_valid(context, etypes[i]) != 0 &&
142 !_kdc_is_weak_exception(princ->entry.principal, etypes[i]))
145 while (hdb_next_enctype2key(context, &princ->entry, etypes[i], &key) == 0) {
146 if (key->key.keyvalue.length == 0) {
147 ret = KRB5KDC_ERR_NULL_KEY;
151 *ret_etype = etypes[i];
153 if (is_default_salt_p(&def_salt, key)) {
154 krb5_free_salt (context, def_salt);
159 krb5_free_salt (context, def_salt);
164 _kdc_make_anonymous_principalname (PrincipalName *pn)
166 pn->name_type = KRB5_NT_PRINCIPAL;
167 pn->name_string.len = 1;
168 pn->name_string.val = malloc(sizeof(*pn->name_string.val));
169 if (pn->name_string.val == NULL)
171 pn->name_string.val[0] = strdup("anonymous");
172 if (pn->name_string.val[0] == NULL) {
173 free(pn->name_string.val);
174 pn->name_string.val = NULL;
181 _kdc_log_timestamp(krb5_context context,
182 krb5_kdc_configuration *config,
184 KerberosTime authtime, KerberosTime *starttime,
185 KerberosTime endtime, KerberosTime *renew_till)
187 char authtime_str[100], starttime_str[100],
188 endtime_str[100], renewtime_str[100];
190 krb5_format_time(context, authtime,
191 authtime_str, sizeof(authtime_str), TRUE);
193 krb5_format_time(context, *starttime,
194 starttime_str, sizeof(starttime_str), TRUE);
196 strlcpy(starttime_str, "unset", sizeof(starttime_str));
197 krb5_format_time(context, endtime,
198 endtime_str, sizeof(endtime_str), TRUE);
200 krb5_format_time(context, *renew_till,
201 renewtime_str, sizeof(renewtime_str), TRUE);
203 strlcpy(renewtime_str, "unset", sizeof(renewtime_str));
205 kdc_log(context, config, 5,
206 "%s authtime: %s starttime: %s endtime: %s renew till: %s",
207 type, authtime_str, starttime_str, endtime_str, renewtime_str);
211 log_patypes(krb5_context context,
212 krb5_kdc_configuration *config,
215 struct rk_strpool *p = NULL;
219 for (i = 0; i < padata->len; i++) {
220 switch(padata->val[i].padata_type) {
221 case KRB5_PADATA_PK_AS_REQ:
222 p = rk_strpoolprintf(p, "PK-INIT(ietf)");
224 case KRB5_PADATA_PK_AS_REQ_WIN:
225 p = rk_strpoolprintf(p, "PK-INIT(win2k)");
227 case KRB5_PADATA_PA_PK_OCSP_RESPONSE:
228 p = rk_strpoolprintf(p, "OCSP");
230 case KRB5_PADATA_ENC_TIMESTAMP:
231 p = rk_strpoolprintf(p, "encrypted-timestamp");
234 p = rk_strpoolprintf(p, "%d", padata->val[i].padata_type);
237 if (p && i + 1 < padata->len)
238 p = rk_strpoolprintf(p, ", ");
240 kdc_log(context, config, 0, "out of memory");
245 p = rk_strpoolprintf(p, "none");
247 str = rk_strpoolcollect(p);
248 kdc_log(context, config, 0, "Client sent patypes: %s", str);
258 _kdc_encode_reply(krb5_context context,
259 krb5_kdc_configuration *config,
260 KDC_REP *rep, const EncTicketPart *et, EncKDCRepPart *ek,
262 int skvno, const EncryptionKey *skey,
263 int ckvno, const EncryptionKey *reply_key,
273 ASN1_MALLOC_ENCODE(EncTicketPart, buf, buf_size, et, &len, ret);
275 kdc_log(context, config, 0, "Failed to encode ticket: %s",
276 krb5_get_err_text(context, ret));
279 if(buf_size != len) {
281 kdc_log(context, config, 0, "Internal error in ASN.1 encoder");
282 *e_text = "KDC internal error";
283 return KRB5KRB_ERR_GENERIC;
286 ret = krb5_crypto_init(context, skey, etype, &crypto);
289 kdc_log(context, config, 0, "krb5_crypto_init failed: %s",
290 krb5_get_err_text(context, ret));
294 ret = krb5_encrypt_EncryptedData(context,
300 &rep->ticket.enc_part);
302 krb5_crypto_destroy(context, crypto);
304 kdc_log(context, config, 0, "Failed to encrypt data: %s",
305 krb5_get_err_text(context, ret));
309 if(rep->msg_type == krb_as_rep && !config->encode_as_rep_as_tgs_rep)
310 ASN1_MALLOC_ENCODE(EncASRepPart, buf, buf_size, ek, &len, ret);
312 ASN1_MALLOC_ENCODE(EncTGSRepPart, buf, buf_size, ek, &len, ret);
314 kdc_log(context, config, 0, "Failed to encode KDC-REP: %s",
315 krb5_get_err_text(context, ret));
318 if(buf_size != len) {
320 kdc_log(context, config, 0, "Internal error in ASN.1 encoder");
321 *e_text = "KDC internal error";
322 return KRB5KRB_ERR_GENERIC;
324 ret = krb5_crypto_init(context, reply_key, 0, &crypto);
327 kdc_log(context, config, 0, "krb5_crypto_init failed: %s",
328 krb5_get_err_text(context, ret));
331 if(rep->msg_type == krb_as_rep) {
332 krb5_encrypt_EncryptedData(context,
334 KRB5_KU_AS_REP_ENC_PART,
340 ASN1_MALLOC_ENCODE(AS_REP, buf, buf_size, rep, &len, ret);
342 krb5_encrypt_EncryptedData(context,
344 KRB5_KU_TGS_REP_ENC_PART_SESSION,
350 ASN1_MALLOC_ENCODE(TGS_REP, buf, buf_size, rep, &len, ret);
352 krb5_crypto_destroy(context, crypto);
354 kdc_log(context, config, 0, "Failed to encode KDC-REP: %s",
355 krb5_get_err_text(context, ret));
358 if(buf_size != len) {
360 kdc_log(context, config, 0, "Internal error in ASN.1 encoder");
361 *e_text = "KDC internal error";
362 return KRB5KRB_ERR_GENERIC;
365 reply->length = buf_size;
370 * Return 1 if the client have only older enctypes, this is for
371 * determining if the server should send ETYPE_INFO2 or not.
375 older_enctype(krb5_enctype enctype)
378 case ETYPE_DES_CBC_CRC:
379 case ETYPE_DES_CBC_MD4:
380 case ETYPE_DES_CBC_MD5:
381 case ETYPE_DES3_CBC_SHA1:
382 case ETYPE_ARCFOUR_HMAC_MD5:
383 case ETYPE_ARCFOUR_HMAC_MD5_56:
385 * The following three is "old" windows enctypes and is needed for
386 * windows 2000 hosts.
388 case ETYPE_ARCFOUR_MD4:
389 case ETYPE_ARCFOUR_HMAC_OLD:
390 case ETYPE_ARCFOUR_HMAC_OLD_EXP:
401 static krb5_error_code
402 make_etype_info_entry(krb5_context context, ETYPE_INFO_ENTRY *ent, Key *key)
404 ent->etype = key->key.keytype;
407 ALLOC(ent->salttype);
409 if(key->salt->type == hdb_pw_salt)
410 *ent->salttype = 0; /* or 1? or NULL? */
411 else if(key->salt->type == hdb_afs3_salt)
414 kdc_log(context, config, 0, "unknown salt-type: %d",
416 return KRB5KRB_ERR_GENERIC;
418 /* according to `the specs', we can't send a salt if
419 we have AFS3 salted key, but that requires that you
420 *know* what cell you are using (e.g by assuming
421 that the cell is the same as the realm in lower
424 ALLOC(ent->salttype);
425 *ent->salttype = key->salt->type;
428 * We shouldn't sent salttype since it is incompatible with the
429 * specification and it breaks windows clients. The afs
430 * salting problem is solved by using KRB5-PADATA-AFS3-SALT
431 * implemented in Heimdal 0.7 and later.
433 ent->salttype = NULL;
435 krb5_copy_data(context, &key->salt->salt,
438 /* we return no salt type at all, as that should indicate
439 * the default salt type and make everybody happy. some
440 * systems (like w2k) dislike being told the salt type
443 ent->salttype = NULL;
449 static krb5_error_code
450 get_pa_etype_info(krb5_context context,
451 krb5_kdc_configuration *config,
452 METHOD_DATA *md, Key *ckey)
454 krb5_error_code ret = 0;
461 pa.val = calloc(1, sizeof(pa.val[0]));
465 ret = make_etype_info_entry(context, &pa.val[0], ckey);
467 free_ETYPE_INFO(&pa);
471 ASN1_MALLOC_ENCODE(ETYPE_INFO, buf, len, &pa, &len, ret);
472 free_ETYPE_INFO(&pa);
475 ret = realloc_method_data(md);
480 md->val[md->len - 1].padata_type = KRB5_PADATA_ETYPE_INFO;
481 md->val[md->len - 1].padata_value.length = len;
482 md->val[md->len - 1].padata_value.data = buf;
490 extern int _krb5_AES_string_to_default_iterator;
492 static krb5_error_code
493 make_etype_info2_entry(ETYPE_INFO2_ENTRY *ent, Key *key)
495 ent->etype = key->key.keytype;
498 if (ent->salt == NULL)
500 *ent->salt = malloc(key->salt->salt.length + 1);
501 if (*ent->salt == NULL) {
506 memcpy(*ent->salt, key->salt->salt.data, key->salt->salt.length);
507 (*ent->salt)[key->salt->salt.length] = '\0';
511 ent->s2kparams = NULL;
513 switch (key->key.keytype) {
514 case ETYPE_AES128_CTS_HMAC_SHA1_96:
515 case ETYPE_AES256_CTS_HMAC_SHA1_96:
516 ALLOC(ent->s2kparams);
517 if (ent->s2kparams == NULL)
519 ent->s2kparams->length = 4;
520 ent->s2kparams->data = malloc(ent->s2kparams->length);
521 if (ent->s2kparams->data == NULL) {
522 free(ent->s2kparams);
523 ent->s2kparams = NULL;
526 _krb5_put_int(ent->s2kparams->data,
527 _krb5_AES_string_to_default_iterator,
528 ent->s2kparams->length);
530 case ETYPE_DES_CBC_CRC:
531 case ETYPE_DES_CBC_MD4:
532 case ETYPE_DES_CBC_MD5:
533 /* Check if this was a AFS3 salted key */
534 if(key->salt && key->salt->type == hdb_afs3_salt){
535 ALLOC(ent->s2kparams);
536 if (ent->s2kparams == NULL)
538 ent->s2kparams->length = 1;
539 ent->s2kparams->data = malloc(ent->s2kparams->length);
540 if (ent->s2kparams->data == NULL) {
541 free(ent->s2kparams);
542 ent->s2kparams = NULL;
545 _krb5_put_int(ent->s2kparams->data,
547 ent->s2kparams->length);
557 * Return an ETYPE-INFO2. Enctypes are storted the same way as in the
558 * database (client supported enctypes first, then the unsupported
562 static krb5_error_code
563 get_pa_etype_info2(krb5_context context,
564 krb5_kdc_configuration *config,
565 METHOD_DATA *md, Key *ckey)
567 krb5_error_code ret = 0;
573 pa.val = calloc(1, sizeof(pa.val[0]));
577 ret = make_etype_info2_entry(&pa.val[0], ckey);
579 free_ETYPE_INFO2(&pa);
583 ASN1_MALLOC_ENCODE(ETYPE_INFO2, buf, len, &pa, &len, ret);
584 free_ETYPE_INFO2(&pa);
587 ret = realloc_method_data(md);
592 md->val[md->len - 1].padata_type = KRB5_PADATA_ETYPE_INFO2;
593 md->val[md->len - 1].padata_value.length = len;
594 md->val[md->len - 1].padata_value.data = buf;
603 log_as_req(krb5_context context,
604 krb5_kdc_configuration *config,
607 const KDC_REQ_BODY *b)
610 struct rk_strpool *p;
614 p = rk_strpoolprintf(NULL, "%s", "Client supported enctypes: ");
616 for (i = 0; i < b->etype.len; i++) {
617 ret = krb5_enctype_to_string(context, b->etype.val[i], &str);
619 p = rk_strpoolprintf(p, "%s", str);
622 p = rk_strpoolprintf(p, "%d", b->etype.val[i]);
623 if (p && i + 1 < b->etype.len)
624 p = rk_strpoolprintf(p, ", ");
626 kdc_log(context, config, 0, "out of memory");
631 p = rk_strpoolprintf(p, "no encryption types");
637 ret = krb5_enctype_to_string(context, cetype, &cet);
639 ret = krb5_enctype_to_string(context, setype, &set);
641 p = rk_strpoolprintf(p, ", using %s/%s", cet, set);
647 p = rk_strpoolprintf(p, ", using enctypes %d/%d",
651 str = rk_strpoolcollect(p);
652 kdc_log(context, config, 0, "%s", str);
657 unparse_flags(KDCOptions2int(b->kdc_options), asn1_KDCOptions_units(),
658 fixedstr, sizeof(fixedstr));
660 kdc_log(context, config, 0, "Requested flags: %s", fixedstr);
665 * verify the flags on `client' and `server', returning 0
666 * if they are OK and generating an error messages and returning
667 * and error code otherwise.
671 kdc_check_flags(krb5_context context,
672 krb5_kdc_configuration *config,
673 hdb_entry_ex *client_ex, const char *client_name,
674 hdb_entry_ex *server_ex, const char *server_name,
675 krb5_boolean is_as_req)
677 if(client_ex != NULL) {
678 hdb_entry *client = &client_ex->entry;
681 if (client->flags.locked_out) {
682 kdc_log(context, config, 0,
683 "Client (%s) is locked out", client_name);
684 return KRB5KDC_ERR_POLICY;
687 if (client->flags.invalid) {
688 kdc_log(context, config, 0,
689 "Client (%s) has invalid bit set", client_name);
690 return KRB5KDC_ERR_POLICY;
693 if(!client->flags.client){
694 kdc_log(context, config, 0,
695 "Principal may not act as client -- %s", client_name);
696 return KRB5KDC_ERR_POLICY;
699 if (client->valid_start && *client->valid_start > kdc_time) {
700 char starttime_str[100];
701 krb5_format_time(context, *client->valid_start,
702 starttime_str, sizeof(starttime_str), TRUE);
703 kdc_log(context, config, 0,
704 "Client not yet valid until %s -- %s",
705 starttime_str, client_name);
706 return KRB5KDC_ERR_CLIENT_NOTYET;
709 if (client->valid_end && *client->valid_end < kdc_time) {
710 char endtime_str[100];
711 krb5_format_time(context, *client->valid_end,
712 endtime_str, sizeof(endtime_str), TRUE);
713 kdc_log(context, config, 0,
714 "Client expired at %s -- %s",
715 endtime_str, client_name);
716 return KRB5KDC_ERR_NAME_EXP;
719 if (client->pw_end && *client->pw_end < kdc_time
720 && (server_ex == NULL || !server_ex->entry.flags.change_pw)) {
722 krb5_format_time(context, *client->pw_end,
723 pwend_str, sizeof(pwend_str), TRUE);
724 kdc_log(context, config, 0,
725 "Client's key has expired at %s -- %s",
726 pwend_str, client_name);
727 return KRB5KDC_ERR_KEY_EXPIRED;
733 if (server_ex != NULL) {
734 hdb_entry *server = &server_ex->entry;
736 if (server->flags.locked_out) {
737 kdc_log(context, config, 0,
738 "Client server locked out -- %s", server_name);
739 return KRB5KDC_ERR_POLICY;
741 if (server->flags.invalid) {
742 kdc_log(context, config, 0,
743 "Server has invalid flag set -- %s", server_name);
744 return KRB5KDC_ERR_POLICY;
747 if(!server->flags.server){
748 kdc_log(context, config, 0,
749 "Principal may not act as server -- %s", server_name);
750 return KRB5KDC_ERR_POLICY;
753 if(!is_as_req && server->flags.initial) {
754 kdc_log(context, config, 0,
755 "AS-REQ is required for server -- %s", server_name);
756 return KRB5KDC_ERR_POLICY;
759 if (server->valid_start && *server->valid_start > kdc_time) {
760 char starttime_str[100];
761 krb5_format_time(context, *server->valid_start,
762 starttime_str, sizeof(starttime_str), TRUE);
763 kdc_log(context, config, 0,
764 "Server not yet valid until %s -- %s",
765 starttime_str, server_name);
766 return KRB5KDC_ERR_SERVICE_NOTYET;
769 if (server->valid_end && *server->valid_end < kdc_time) {
770 char endtime_str[100];
771 krb5_format_time(context, *server->valid_end,
772 endtime_str, sizeof(endtime_str), TRUE);
773 kdc_log(context, config, 0,
774 "Server expired at %s -- %s",
775 endtime_str, server_name);
776 return KRB5KDC_ERR_SERVICE_EXP;
779 if (server->pw_end && *server->pw_end < kdc_time) {
781 krb5_format_time(context, *server->pw_end,
782 pwend_str, sizeof(pwend_str), TRUE);
783 kdc_log(context, config, 0,
784 "Server's key has expired at -- %s",
785 pwend_str, server_name);
786 return KRB5KDC_ERR_KEY_EXPIRED;
793 * Return TRUE if `from' is part of `addresses' taking into consideration
794 * the configuration variables that tells us how strict we should be about
799 _kdc_check_addresses(krb5_context context,
800 krb5_kdc_configuration *config,
801 HostAddresses *addresses, const struct sockaddr *from)
806 krb5_boolean only_netbios = TRUE;
809 if(config->check_ticket_addresses == 0)
812 if(addresses == NULL)
813 return config->allow_null_ticket_addresses;
815 for (i = 0; i < addresses->len; ++i) {
816 if (addresses->val[i].addr_type != KRB5_ADDRESS_NETBIOS) {
817 only_netbios = FALSE;
821 /* Windows sends it's netbios name, which I can only assume is
822 * used for the 'allowed workstations' check. This is painful,
823 * but we still want to check IP addresses if they happen to be
828 return config->allow_null_ticket_addresses;
830 ret = krb5_sockaddr2address (context, from, &addr);
834 result = krb5_address_search(context, &addr, addresses);
835 krb5_free_address (context, &addr);
844 send_pac_p(krb5_context context, KDC_REQ *req)
847 PA_PAC_REQUEST pacreq;
851 pa = _kdc_find_padata(req, &i, KRB5_PADATA_PA_PAC_REQUEST);
855 ret = decode_PA_PAC_REQUEST(pa->padata_value.data,
856 pa->padata_value.length,
861 i = pacreq.include_pac;
862 free_PA_PAC_REQUEST(&pacreq);
869 _kdc_is_anonymous(krb5_context context, krb5_principal principal)
871 if (principal->name.name_type != KRB5_NT_WELLKNOWN ||
872 principal->name.name_string.len != 2 ||
873 strcmp(principal->name.name_string.val[0], KRB5_WELLKNOWN_NAME) != 0 ||
874 strcmp(principal->name.name_string.val[1], KRB5_ANON_NAME) != 0)
884 _kdc_as_rep(krb5_context context,
885 krb5_kdc_configuration *config,
887 const krb5_data *req_buffer,
890 struct sockaddr *from_addr,
893 KDC_REQ_BODY *b = &req->req_body;
895 KDCOptions f = b->kdc_options;
896 hdb_entry_ex *client = NULL, *server = NULL;
898 krb5_enctype cetype, setype, sessionetype;
902 krb5_principal client_princ = NULL, server_princ = NULL;
903 char *client_name = NULL, *server_name = NULL;
904 krb5_error_code ret = 0;
905 const char *e_text = NULL;
908 EncryptionKey *reply_key;
911 pk_client_params *pkp = NULL;
914 memset(&rep, 0, sizeof(rep));
915 krb5_data_zero(&e_data);
918 flags |= HDB_F_CANON;
920 if(b->sname == NULL){
921 ret = KRB5KRB_ERR_GENERIC;
922 e_text = "No server in request";
924 ret = _krb5_principalname2krb5_principal (context,
929 ret = krb5_unparse_name(context, server_princ, &server_name);
932 kdc_log(context, config, 0,
933 "AS-REQ malformed server name from %s", from);
936 if(b->cname == NULL){
937 ret = KRB5KRB_ERR_GENERIC;
938 e_text = "No client in request";
940 ret = _krb5_principalname2krb5_principal (context,
947 ret = krb5_unparse_name(context, client_princ, &client_name);
950 kdc_log(context, config, 0,
951 "AS-REQ malformed client name from %s", from);
955 kdc_log(context, config, 0, "AS-REQ %s from %s for %s",
956 client_name, from, server_name);
962 if (_kdc_is_anonymous(context, client_princ)) {
963 if (!b->kdc_options.request_anonymous) {
964 kdc_log(context, config, 0, "Anonymous ticket w/o anonymous flag");
965 ret = KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN;
968 } else if (b->kdc_options.request_anonymous) {
969 kdc_log(context, config, 0,
970 "Request for a anonymous ticket with non "
971 "anonymous client name: %s", client_name);
972 ret = KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN;
980 ret = _kdc_db_fetch(context, config, client_princ,
981 HDB_F_GET_CLIENT | flags, &clientdb, &client);
983 kdc_log(context, config, 0, "UNKNOWN -- %s: %s", client_name,
984 krb5_get_err_text(context, ret));
985 ret = KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN;
989 ret = _kdc_db_fetch(context, config, server_princ,
990 HDB_F_GET_SERVER|HDB_F_GET_KRBTGT,
993 kdc_log(context, config, 0, "UNKNOWN -- %s: %s", server_name,
994 krb5_get_err_text(context, ret));
995 ret = KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN;
999 memset(&et, 0, sizeof(et));
1000 memset(&ek, 0, sizeof(ek));
1003 * Find the client key for reply encryption and pa-type salt, Pick
1004 * the client key upfront before the other keys because that is
1005 * going to affect what enctypes we are going to use in
1009 ret = _kdc_find_etype(context, client, b->etype.val, b->etype.len,
1012 kdc_log(context, config, 0,
1013 "Client (%s) has no support for etypes", client_name);
1018 * Pre-auth processing
1026 log_patypes(context, config, req->padata);
1029 kdc_log(context, config, 5,
1030 "Looking for PKINIT pa-data -- %s", client_name);
1032 e_text = "No PKINIT PA found";
1035 pa = _kdc_find_padata(req, &i, KRB5_PADATA_PK_AS_REQ);
1038 pa = _kdc_find_padata(req, &i, KRB5_PADATA_PK_AS_REQ_WIN);
1041 char *client_cert = NULL;
1043 ret = _kdc_pk_rd_padata(context, config, req, pa, client, &pkp);
1045 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
1046 kdc_log(context, config, 5,
1047 "Failed to decode PKINIT PA-DATA -- %s",
1051 if (ret == 0 && pkp == NULL)
1054 ret = _kdc_pk_check_client(context,
1060 e_text = "PKINIT certificate not allowed to "
1061 "impersonate principal";
1062 _kdc_pk_free_client_param(context, pkp);
1064 kdc_log(context, config, 0, "%s", e_text);
1070 et.flags.pre_authent = 1;
1071 kdc_log(context, config, 0,
1072 "PKINIT pre-authentication succeeded -- %s using %s",
1073 client_name, client_cert);
1080 kdc_log(context, config, 5, "Looking for ENC-TS pa-data -- %s",
1084 e_text = "No ENC-TS found";
1085 while((pa = _kdc_find_padata(req, &i, KRB5_PADATA_ENC_TIMESTAMP))){
1089 EncryptedData enc_data;
1095 if (b->kdc_options.request_anonymous) {
1096 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
1097 kdc_log(context, config, 0, "ENC-TS doesn't support anon");
1101 ret = decode_EncryptedData(pa->padata_value.data,
1102 pa->padata_value.length,
1106 ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
1107 kdc_log(context, config, 5, "Failed to decode PA-DATA -- %s",
1112 ret = hdb_enctype2key(context, &client->entry,
1113 enc_data.etype, &pa_key);
1116 e_text = "No key matches pa-data";
1117 ret = KRB5KDC_ERR_ETYPE_NOSUPP;
1118 if(krb5_enctype_to_string(context, enc_data.etype, &estr))
1121 kdc_log(context, config, 5,
1122 "No client key matching pa-data (%d) -- %s",
1123 enc_data.etype, client_name);
1125 kdc_log(context, config, 5,
1126 "No client key matching pa-data (%s) -- %s",
1129 free_EncryptedData(&enc_data);
1135 ret = krb5_crypto_init(context, &pa_key->key, 0, &crypto);
1137 kdc_log(context, config, 0, "krb5_crypto_init failed: %s",
1138 krb5_get_err_text(context, ret));
1139 free_EncryptedData(&enc_data);
1143 ret = krb5_decrypt_EncryptedData (context,
1145 KRB5_KU_PA_ENC_TIMESTAMP,
1148 krb5_crypto_destroy(context, crypto);
1150 * Since the user might have several keys with the same
1151 * enctype but with diffrent salting, we need to try all
1152 * the keys with the same enctype.
1155 krb5_error_code ret2;
1156 ret2 = krb5_enctype_to_string(context,
1157 pa_key->key.keytype, &str);
1160 kdc_log(context, config, 5,
1161 "Failed to decrypt PA-DATA -- %s "
1162 "(enctype %s) error %s",
1164 str ? str : "unknown enctype",
1165 krb5_get_err_text(context, ret));
1168 if(hdb_next_enctype2key(context, &client->entry,
1169 enc_data.etype, &pa_key) == 0)
1171 e_text = "Failed to decrypt PA-DATA";
1173 free_EncryptedData(&enc_data);
1175 if (clientdb->hdb_auth_status)
1176 (clientdb->hdb_auth_status)(context, clientdb, client, HDB_AUTH_WRONG_PASSWORD);
1178 ret = KRB5KDC_ERR_PREAUTH_FAILED;
1181 free_EncryptedData(&enc_data);
1182 ret = decode_PA_ENC_TS_ENC(ts_data.data,
1186 krb5_data_free(&ts_data);
1188 e_text = "Failed to decode PA-ENC-TS-ENC";
1189 ret = KRB5KDC_ERR_PREAUTH_FAILED;
1190 kdc_log(context, config,
1191 5, "Failed to decode PA-ENC-TS_ENC -- %s",
1195 free_PA_ENC_TS_ENC(&p);
1196 if (abs(kdc_time - p.patimestamp) > context->max_skew) {
1197 char client_time[100];
1199 krb5_format_time(context, p.patimestamp,
1200 client_time, sizeof(client_time), TRUE);
1202 ret = KRB5KRB_AP_ERR_SKEW;
1203 kdc_log(context, config, 0,
1204 "Too large time skew, "
1205 "client time %s is out by %u > %u seconds -- %s",
1207 (unsigned)abs(kdc_time - p.patimestamp),
1211 /* This code is from samba, needs testing */
1213 * the following is needed to make windows clients
1214 * to retry using the timestamp in the error message
1216 * this is maybe a bug in windows to not trying when e_text
1221 e_text = "Too large time skew";
1225 et.flags.pre_authent = 1;
1227 ret = krb5_enctype_to_string(context,pa_key->key.keytype, &str);
1231 kdc_log(context, config, 2,
1232 "ENC-TS Pre-authentication succeeded -- %s using %s",
1233 client_name, str ? str : "unknown enctype");
1240 if(found_pa == 0 && config->require_preauth)
1242 /* We come here if we found a pa-enc-timestamp, but if there
1243 was some problem with it, other than too large skew */
1244 if(found_pa && et.flags.pre_authent == 0){
1245 kdc_log(context, config, 0, "%s -- %s", e_text, client_name);
1249 }else if (config->require_preauth
1250 || b->kdc_options.request_anonymous /* hack to force anon */
1251 || client->entry.flags.require_preauth
1252 || server->entry.flags.require_preauth) {
1253 METHOD_DATA method_data;
1259 method_data.len = 0;
1260 method_data.val = NULL;
1262 ret = realloc_method_data(&method_data);
1264 free_METHOD_DATA(&method_data);
1267 pa = &method_data.val[method_data.len-1];
1268 pa->padata_type = KRB5_PADATA_ENC_TIMESTAMP;
1269 pa->padata_value.length = 0;
1270 pa->padata_value.data = NULL;
1273 ret = realloc_method_data(&method_data);
1275 free_METHOD_DATA(&method_data);
1278 pa = &method_data.val[method_data.len-1];
1279 pa->padata_type = KRB5_PADATA_PK_AS_REQ;
1280 pa->padata_value.length = 0;
1281 pa->padata_value.data = NULL;
1283 ret = realloc_method_data(&method_data);
1285 free_METHOD_DATA(&method_data);
1288 pa = &method_data.val[method_data.len-1];
1289 pa->padata_type = KRB5_PADATA_PK_AS_REQ_WIN;
1290 pa->padata_value.length = 0;
1291 pa->padata_value.data = NULL;
1295 * If there is a client key, send ETYPE_INFO{,2}
1301 * - If the client only knows about old enctypes, then send
1302 * both info replies (we send 'info' first in the list).
1303 * - If the client is 'modern', because it knows about 'new'
1304 * enctype types, then only send the 'info2' reply.
1306 * Before we send the full list of etype-info data, we pick
1307 * the client key we would have used anyway below, just pick
1311 if (older_enctype(ckey->key.keytype)) {
1312 ret = get_pa_etype_info(context, config,
1313 &method_data, ckey);
1315 free_METHOD_DATA(&method_data);
1319 ret = get_pa_etype_info2(context, config,
1320 &method_data, ckey);
1322 free_METHOD_DATA(&method_data);
1327 ASN1_MALLOC_ENCODE(METHOD_DATA, buf, len, &method_data, &len, ret);
1328 free_METHOD_DATA(&method_data);
1331 e_data.length = len;
1332 e_text ="Need to use PA-ENC-TIMESTAMP/PA-PK-AS-REQ",
1334 ret = KRB5KDC_ERR_PREAUTH_REQUIRED;
1336 kdc_log(context, config, 0,
1337 "No preauth found, returning PREAUTH-REQUIRED -- %s",
1342 if (clientdb->hdb_auth_status)
1343 (clientdb->hdb_auth_status)(context, clientdb, client,
1347 * Verify flags after the user been required to prove its identity
1348 * with in a preauth mech.
1351 ret = _kdc_check_access(context, config, client, client_name,
1352 server, server_name,
1358 * Selelct the best encryption type for the KDC with out regard to
1359 * the client since the client never needs to read that data.
1362 ret = _kdc_get_preferred_key(context, config,
1363 server, server_name,
1369 * Select a session enctype from the list of the crypto systems
1370 * supported enctype, is supported by the client and is one of the
1371 * enctype of the enctype of the krbtgt.
1373 * The later is used as a hint what enctype all KDC are supporting
1374 * to make sure a newer version of KDC wont generate a session
1375 * enctype that and older version of a KDC in the same realm can't
1378 * But if the KDC admin is paranoid and doesn't want to have "no
1379 * the best" enctypes on the krbtgt, lets save the best pick from
1380 * the client list and hope that that will work for any other
1384 const krb5_enctype *p;
1385 krb5_enctype clientbest = ETYPE_NULL;
1388 p = krb5_kerberos_enctypes(context);
1390 sessionetype = ETYPE_NULL;
1392 for (i = 0; p[i] != ETYPE_NULL && sessionetype == ETYPE_NULL; i++) {
1393 if (krb5_enctype_valid(context, p[i]) != 0)
1396 for (j = 0; j < b->etype.len && sessionetype == ETYPE_NULL; j++) {
1398 /* check with client */
1399 if (p[i] != b->etype.val[j])
1401 /* save best of union of { client, crypto system } */
1402 if (clientbest == ETYPE_NULL)
1404 /* check with krbtgt */
1405 ret = hdb_enctype2key(context, &server->entry, p[i], &dummy);
1408 sessionetype = p[i];
1411 /* if krbtgt had no shared keys with client, pick clients best */
1412 if (clientbest != ETYPE_NULL && sessionetype == ETYPE_NULL) {
1413 sessionetype = clientbest;
1414 } else if (sessionetype == ETYPE_NULL) {
1415 kdc_log(context, config, 0,
1416 "Client (%s) from %s has no common enctypes with KDC"
1417 "to use for the session key",
1423 log_as_req(context, config, cetype, setype, b);
1425 if(f.renew || f.validate || f.proxy || f.forwarded || f.enc_tkt_in_skey
1426 || (f.request_anonymous && !config->allow_anonymous)) {
1427 ret = KRB5KDC_ERR_BADOPTION;
1428 kdc_log(context, config, 0, "Bad KDC options -- %s", client_name);
1433 rep.msg_type = krb_as_rep;
1435 ret = copy_Realm(&client->entry.principal->realm, &rep.crealm);
1438 ret = _krb5_principal2principalname(&rep.cname, client->entry.principal);
1442 rep.ticket.tkt_vno = 5;
1443 copy_Realm(&server->entry.principal->realm, &rep.ticket.realm);
1444 _krb5_principal2principalname(&rep.ticket.sname,
1445 server->entry.principal);
1446 /* java 1.6 expects the name to be the same type, lets allow that
1447 * uncomplicated name-types. */
1448 #define CNT(sp,t) (((sp)->sname->name_type) == KRB5_NT_##t)
1449 if (CNT(b, UNKNOWN) || CNT(b, PRINCIPAL) || CNT(b, SRV_INST) || CNT(b, SRV_HST) || CNT(b, SRV_XHST))
1450 rep.ticket.sname.name_type = b->sname->name_type;
1453 et.flags.initial = 1;
1454 if(client->entry.flags.forwardable && server->entry.flags.forwardable)
1455 et.flags.forwardable = f.forwardable;
1456 else if (f.forwardable) {
1457 ret = KRB5KDC_ERR_POLICY;
1458 kdc_log(context, config, 0,
1459 "Ticket may not be forwardable -- %s", client_name);
1462 if(client->entry.flags.proxiable && server->entry.flags.proxiable)
1463 et.flags.proxiable = f.proxiable;
1464 else if (f.proxiable) {
1465 ret = KRB5KDC_ERR_POLICY;
1466 kdc_log(context, config, 0,
1467 "Ticket may not be proxiable -- %s", client_name);
1470 if(client->entry.flags.postdate && server->entry.flags.postdate)
1471 et.flags.may_postdate = f.allow_postdate;
1472 else if (f.allow_postdate){
1473 ret = KRB5KDC_ERR_POLICY;
1474 kdc_log(context, config, 0,
1475 "Ticket may not be postdatable -- %s", client_name);
1479 /* check for valid set of addresses */
1480 if(!_kdc_check_addresses(context, config, b->addresses, from_addr)) {
1481 ret = KRB5KRB_AP_ERR_BADADDR;
1482 kdc_log(context, config, 0,
1483 "Bad address list requested -- %s", client_name);
1487 ret = copy_PrincipalName(&rep.cname, &et.cname);
1490 ret = copy_Realm(&rep.crealm, &et.crealm);
1498 start = et.authtime = kdc_time;
1500 if(f.postdated && req->req_body.from){
1501 ALLOC(et.starttime);
1502 start = *et.starttime = *req->req_body.from;
1503 et.flags.invalid = 1;
1504 et.flags.postdated = 1; /* XXX ??? */
1506 _kdc_fix_time(&b->till);
1509 /* be careful not overflowing */
1511 if(client->entry.max_life)
1512 t = start + min(t - start, *client->entry.max_life);
1513 if(server->entry.max_life)
1514 t = start + min(t - start, *server->entry.max_life);
1516 t = min(t, start + realm->max_life);
1519 if(f.renewable_ok && et.endtime < *b->till){
1521 if(b->rtime == NULL){
1525 if(*b->rtime < *b->till)
1526 *b->rtime = *b->till;
1528 if(f.renewable && b->rtime){
1532 if(client->entry.max_renew)
1533 t = start + min(t - start, *client->entry.max_renew);
1534 if(server->entry.max_renew)
1535 t = start + min(t - start, *server->entry.max_renew);
1537 t = min(t, start + realm->max_renew);
1539 ALLOC(et.renew_till);
1541 et.flags.renewable = 1;
1545 if (f.request_anonymous)
1546 et.flags.anonymous = 1;
1550 copy_HostAddresses(b->addresses, et.caddr);
1553 et.transited.tr_type = DOMAIN_X500_COMPRESS;
1554 krb5_data_zero(&et.transited.contents);
1556 /* The MIT ASN.1 library (obviously) doesn't tell lengths encoded
1557 * as 0 and as 0x80 (meaning indefinite length) apart, and is thus
1558 * incapable of correctly decoding SEQUENCE OF's of zero length.
1560 * To fix this, always send at least one no-op last_req
1562 * If there's a pw_end or valid_end we will use that,
1563 * otherwise just a dummy lr.
1565 ek.last_req.val = malloc(2 * sizeof(*ek.last_req.val));
1566 if (ek.last_req.val == NULL) {
1570 ek.last_req.len = 0;
1571 if (client->entry.pw_end
1572 && (config->kdc_warn_pwexpire == 0
1573 || kdc_time + config->kdc_warn_pwexpire >= *client->entry.pw_end)) {
1574 ek.last_req.val[ek.last_req.len].lr_type = LR_PW_EXPTIME;
1575 ek.last_req.val[ek.last_req.len].lr_value = *client->entry.pw_end;
1578 if (client->entry.valid_end) {
1579 ek.last_req.val[ek.last_req.len].lr_type = LR_ACCT_EXPTIME;
1580 ek.last_req.val[ek.last_req.len].lr_value = *client->entry.valid_end;
1583 if (ek.last_req.len == 0) {
1584 ek.last_req.val[ek.last_req.len].lr_type = LR_NONE;
1585 ek.last_req.val[ek.last_req.len].lr_value = 0;
1588 ek.nonce = b->nonce;
1589 if (client->entry.valid_end || client->entry.pw_end) {
1590 ALLOC(ek.key_expiration);
1591 if (client->entry.valid_end) {
1592 if (client->entry.pw_end)
1593 *ek.key_expiration = min(*client->entry.valid_end,
1594 *client->entry.pw_end);
1596 *ek.key_expiration = *client->entry.valid_end;
1598 *ek.key_expiration = *client->entry.pw_end;
1600 ek.key_expiration = NULL;
1601 ek.flags = et.flags;
1602 ek.authtime = et.authtime;
1604 ALLOC(ek.starttime);
1605 *ek.starttime = *et.starttime;
1607 ek.endtime = et.endtime;
1608 if (et.renew_till) {
1609 ALLOC(ek.renew_till);
1610 *ek.renew_till = *et.renew_till;
1612 copy_Realm(&rep.ticket.realm, &ek.srealm);
1613 copy_PrincipalName(&rep.ticket.sname, &ek.sname);
1616 copy_HostAddresses(et.caddr, ek.caddr);
1620 rep.padata->len = 0;
1621 rep.padata->val = NULL;
1625 e_text = "Failed to build PK-INIT reply";
1626 ret = _kdc_pk_mk_pa_reply(context, config, pkp, client,
1627 sessionetype, req, req_buffer,
1628 &reply_key, &et.key, rep.padata);
1631 ret = _kdc_add_inital_verified_cas(context,
1640 reply_key = &ckey->key;
1641 ret = krb5_generate_random_keyblock(context, sessionetype, &et.key);
1645 e_text = "Client have no reply key";
1646 ret = KRB5KDC_ERR_CLIENT_NOTYET;
1650 ret = copy_EncryptionKey(&et.key, &ek.key);
1655 set_salt_padata (rep.padata, ckey->salt);
1657 /* Add signing of alias referral */
1658 if (f.canonicalize) {
1659 PA_ClientCanonicalized canon;
1665 memset(&canon, 0, sizeof(canon));
1667 canon.names.requested_name = *b->cname;
1668 canon.names.mapped_name = client->entry.principal->name;
1670 ASN1_MALLOC_ENCODE(PA_ClientCanonicalizedNames, data.data, data.length,
1671 &canon.names, &len, ret);
1674 if (data.length != len)
1675 krb5_abortx(context, "internal asn.1 error");
1677 /* sign using "returned session key" */
1678 ret = krb5_crypto_init(context, &et.key, 0, &crypto);
1684 ret = krb5_create_checksum(context, crypto,
1685 KRB5_KU_CANONICALIZED_NAMES, 0,
1686 data.data, data.length,
1687 &canon.canon_checksum);
1689 krb5_crypto_destroy(context, crypto);
1693 ASN1_MALLOC_ENCODE(PA_ClientCanonicalized, data.data, data.length,
1695 free_Checksum(&canon.canon_checksum);
1698 if (data.length != len)
1699 krb5_abortx(context, "internal asn.1 error");
1701 pa.padata_type = KRB5_PADATA_CLIENT_CANONICALIZED;
1702 pa.padata_value = data;
1703 ret = add_METHOD_DATA(rep.padata, &pa);
1709 if (rep.padata->len == 0) {
1715 if (send_pac_p(context, req)) {
1719 ret = _kdc_pac_generate(context, client, &p);
1721 kdc_log(context, config, 0, "PAC generation failed for -- %s",
1726 ret = _krb5_pac_sign(context, p, et.authtime,
1727 client->entry.principal,
1728 &skey->key, /* Server key */
1729 &skey->key, /* FIXME: should be krbtgt key */
1731 krb5_pac_free(context, p);
1733 kdc_log(context, config, 0, "PAC signing failed for -- %s",
1738 ret = _kdc_tkt_add_if_relevant_ad(context, &et,
1739 KRB5_AUTHDATA_WIN2K_PAC,
1741 krb5_data_free(&data);
1747 _kdc_log_timestamp(context, config, "AS-REQ", et.authtime, et.starttime,
1748 et.endtime, et.renew_till);
1750 /* do this as the last thing since this signs the EncTicketPart */
1751 ret = _kdc_add_KRB5SignedPath(context,
1761 ret = _kdc_encode_reply(context, config,
1762 &rep, &et, &ek, setype, server->entry.kvno,
1763 &skey->key, client->entry.kvno,
1764 reply_key, &e_text, reply);
1765 free_EncTicketPart(&et);
1766 free_EncKDCRepPart(&ek);
1771 if (datagram_reply && reply->length > config->max_datagram_reply_length) {
1772 krb5_data_free(reply);
1773 ret = KRB5KRB_ERR_RESPONSE_TOO_BIG;
1774 e_text = "Reply packet too large";
1780 krb5_mk_error(context,
1783 (e_data.data ? &e_data : NULL),
1793 _kdc_pk_free_client_param(context, pkp);
1798 krb5_free_principal(context, client_princ);
1801 krb5_free_principal(context, server_princ);
1804 _kdc_free_ent(context, client);
1806 _kdc_free_ent(context, server);
1811 * Add the AuthorizationData `data´ of `type´ to the last element in
1812 * the sequence of authorization_data in `tkt´ wrapped in an IF_RELEVANT
1816 _kdc_tkt_add_if_relevant_ad(krb5_context context,
1819 const krb5_data *data)
1821 krb5_error_code ret;
1824 if (tkt->authorization_data == NULL) {
1825 tkt->authorization_data = calloc(1, sizeof(*tkt->authorization_data));
1826 if (tkt->authorization_data == NULL) {
1827 krb5_set_error_message(context, ENOMEM, "out of memory");
1832 /* add the entry to the last element */
1834 AuthorizationData ad = { 0, NULL };
1835 AuthorizationDataElement ade;
1838 ade.ad_data = *data;
1840 ret = add_AuthorizationData(&ad, &ade);
1842 krb5_set_error_message(context, ret, "add AuthorizationData failed");
1846 ade.ad_type = KRB5_AUTHDATA_IF_RELEVANT;
1848 ASN1_MALLOC_ENCODE(AuthorizationData,
1849 ade.ad_data.data, ade.ad_data.length,
1851 free_AuthorizationData(&ad);
1853 krb5_set_error_message(context, ret, "ASN.1 encode of "
1854 "AuthorizationData failed");
1857 if (ade.ad_data.length != size)
1858 krb5_abortx(context, "internal asn.1 encoder error");
1860 ret = add_AuthorizationData(tkt->authorization_data, &ade);
1861 der_free_octet_string(&ade.ad_data);
1863 krb5_set_error_message(context, ret, "add AuthorizationData failed");