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 #define MAX_TIME ((time_t)((1U << 31) - 1))
39 _kdc_fix_time(time_t **t
)
45 if(**t
== 0) **t
= MAX_TIME
; /* fix for old clients */
49 realloc_method_data(METHOD_DATA
*md
)
52 pa
= realloc(md
->val
, (md
->len
+ 1) * sizeof(*md
->val
));
61 set_salt_padata(METHOD_DATA
*md
, Salt
*salt
)
64 realloc_method_data(md
);
65 md
->val
[md
->len
- 1].padata_type
= salt
->type
;
66 der_copy_octet_string(&salt
->salt
,
67 &md
->val
[md
->len
- 1].padata_value
);
72 _kdc_find_padata(const KDC_REQ
*req
, int *start
, int type
)
74 if (req
->padata
== NULL
)
77 while((size_t)*start
< req
->padata
->len
){
79 if(req
->padata
->val
[*start
- 1].padata_type
== (unsigned)type
)
80 return &req
->padata
->val
[*start
- 1];
86 * This is a hack to allow predefined weak services, like afs to
87 * still use weak types
91 _kdc_is_weak_exception(krb5_principal principal
, krb5_enctype etype
)
93 if (principal
->name
.name_string
.len
> 0 &&
94 strcmp(principal
->name
.name_string
.val
[0], "afs") == 0 &&
95 (etype
== ETYPE_DES_CBC_CRC
96 || etype
== ETYPE_DES_CBC_MD4
97 || etype
== ETYPE_DES_CBC_MD5
))
104 * Detect if `key' is the using the the precomputed `default_salt'.
108 is_default_salt_p(const krb5_salt
*default_salt
, const Key
*key
)
110 if (key
->salt
== NULL
)
112 if (default_salt
->salttype
!= key
->salt
->type
)
114 if (krb5_data_cmp(&default_salt
->saltvalue
, &key
->salt
->salt
))
120 * return the first appropriate key of `princ' in `ret_key'. Look for
121 * all the etypes in (`etypes', `len'), stopping as soon as we find
122 * one, but preferring one that has default salt
126 _kdc_find_etype(krb5_context context
, krb5_boolean use_strongest_session_key
,
127 krb5_boolean is_preauth
, hdb_entry_ex
*princ
,
128 krb5_enctype
*etypes
, unsigned len
,
129 krb5_enctype
*ret_enctype
, Key
**ret_key
)
133 krb5_enctype enctype
= ETYPE_NULL
;
137 /* We'll want to avoid keys with v4 salted keys in the pre-auth case... */
138 ret
= krb5_get_pw_salt(context
, princ
->entry
.principal
, &def_salt
);
142 ret
= KRB5KDC_ERR_ETYPE_NOSUPP
;
144 if (use_strongest_session_key
) {
145 const krb5_enctype
*p
;
146 krb5_enctype clientbest
= ETYPE_NULL
;
150 * Pick the strongest key that the KDC, target service, and
151 * client all support, using the local cryptosystem enctype
152 * list in strongest-to-weakest order to drive the search.
154 * This is not what RFC4120 says to do, but it encourages
155 * adoption of stronger enctypes. This doesn't play well with
156 * clients that have multiple Kerberos client implementations
157 * available with different supported enctype lists.
160 /* drive the search with local supported enctypes list */
161 p
= krb5_kerberos_enctypes(context
);
162 for (i
= 0; p
[i
] != ETYPE_NULL
&& enctype
== ETYPE_NULL
; i
++) {
163 if (krb5_enctype_valid(context
, p
[i
]) != 0)
166 /* check that the client supports it too */
167 for (j
= 0; j
< len
&& enctype
== ETYPE_NULL
; j
++) {
168 if (p
[i
] != etypes
[j
])
170 /* save best of union of { client, crypto system } */
171 if (clientbest
== ETYPE_NULL
)
173 /* check target princ support */
174 ret
= hdb_enctype2key(context
, &princ
->entry
, p
[i
], &key
);
177 if (is_preauth
&& !is_default_salt_p(&def_salt
, key
))
182 if (clientbest
!= ETYPE_NULL
&& enctype
== ETYPE_NULL
)
183 enctype
= clientbest
;
184 else if (enctype
== ETYPE_NULL
)
185 ret
= KRB5KDC_ERR_ETYPE_NOSUPP
;
186 if (ret
== 0 && ret_enctype
!= NULL
)
187 *ret_enctype
= enctype
;
188 if (ret
== 0 && ret_key
!= NULL
)
192 * Pick the first key from the client's enctype list that is
193 * supported by the cryptosystem and by the given principal.
195 * RFC4120 says we SHOULD pick the first _strong_ key from the
196 * client's list... not the first key... If the admin disallows
197 * weak enctypes in krb5.conf and selects this key selection
198 * algorithm, then we get exactly what RFC4120 says.
200 for(key
= NULL
, i
= 0; ret
!= 0 && i
< len
; i
++, key
= NULL
) {
202 if (krb5_enctype_valid(context
, etypes
[i
]) != 0 &&
203 !_kdc_is_weak_exception(princ
->entry
.principal
, etypes
[i
]))
206 while (hdb_next_enctype2key(context
, &princ
->entry
, etypes
[i
], &key
) == 0) {
207 if (key
->key
.keyvalue
.length
== 0) {
208 ret
= KRB5KDC_ERR_NULL_KEY
;
213 if (ret_enctype
!= NULL
)
214 *ret_enctype
= etypes
[i
];
216 if (is_preauth
&& is_default_salt_p(&def_salt
, key
))
223 krb5_free_salt (context
, def_salt
);
228 _kdc_make_anonymous_principalname (PrincipalName
*pn
)
230 pn
->name_type
= KRB5_NT_PRINCIPAL
;
231 pn
->name_string
.len
= 1;
232 pn
->name_string
.val
= malloc(sizeof(*pn
->name_string
.val
));
233 if (pn
->name_string
.val
== NULL
)
235 pn
->name_string
.val
[0] = strdup("anonymous");
236 if (pn
->name_string
.val
[0] == NULL
) {
237 free(pn
->name_string
.val
);
238 pn
->name_string
.val
= NULL
;
245 _kdc_log_timestamp(krb5_context context
,
246 krb5_kdc_configuration
*config
,
248 KerberosTime authtime
, KerberosTime
*starttime
,
249 KerberosTime endtime
, KerberosTime
*renew_till
)
251 char authtime_str
[100], starttime_str
[100],
252 endtime_str
[100], renewtime_str
[100];
254 krb5_format_time(context
, authtime
,
255 authtime_str
, sizeof(authtime_str
), TRUE
);
257 krb5_format_time(context
, *starttime
,
258 starttime_str
, sizeof(starttime_str
), TRUE
);
260 strlcpy(starttime_str
, "unset", sizeof(starttime_str
));
261 krb5_format_time(context
, endtime
,
262 endtime_str
, sizeof(endtime_str
), TRUE
);
264 krb5_format_time(context
, *renew_till
,
265 renewtime_str
, sizeof(renewtime_str
), TRUE
);
267 strlcpy(renewtime_str
, "unset", sizeof(renewtime_str
));
269 kdc_log(context
, config
, 5,
270 "%s authtime: %s starttime: %s endtime: %s renew till: %s",
271 type
, authtime_str
, starttime_str
, endtime_str
, renewtime_str
);
275 log_patypes(krb5_context context
,
276 krb5_kdc_configuration
*config
,
279 struct rk_strpool
*p
= NULL
;
283 for (i
= 0; i
< padata
->len
; i
++) {
284 switch(padata
->val
[i
].padata_type
) {
285 case KRB5_PADATA_PK_AS_REQ
:
286 p
= rk_strpoolprintf(p
, "PK-INIT(ietf)");
288 case KRB5_PADATA_PK_AS_REQ_WIN
:
289 p
= rk_strpoolprintf(p
, "PK-INIT(win2k)");
291 case KRB5_PADATA_PA_PK_OCSP_RESPONSE
:
292 p
= rk_strpoolprintf(p
, "OCSP");
294 case KRB5_PADATA_ENC_TIMESTAMP
:
295 p
= rk_strpoolprintf(p
, "encrypted-timestamp");
298 p
= rk_strpoolprintf(p
, "%d", padata
->val
[i
].padata_type
);
301 if (p
&& i
+ 1 < padata
->len
)
302 p
= rk_strpoolprintf(p
, ", ");
304 kdc_log(context
, config
, 0, "out of memory");
309 p
= rk_strpoolprintf(p
, "none");
311 str
= rk_strpoolcollect(p
);
312 kdc_log(context
, config
, 0, "Client sent patypes: %s", str
);
322 _kdc_encode_reply(krb5_context context
,
323 krb5_kdc_configuration
*config
,
324 KDC_REP
*rep
, const EncTicketPart
*et
, EncKDCRepPart
*ek
,
326 int skvno
, const EncryptionKey
*skey
,
327 int ckvno
, const EncryptionKey
*reply_key
,
338 ASN1_MALLOC_ENCODE(EncTicketPart
, buf
, buf_size
, et
, &len
, ret
);
340 const char *msg
= krb5_get_error_message(context
, ret
);
341 kdc_log(context
, config
, 0, "Failed to encode ticket: %s", msg
);
342 krb5_free_error_message(context
, msg
);
345 if(buf_size
!= len
) {
347 kdc_log(context
, config
, 0, "Internal error in ASN.1 encoder");
348 *e_text
= "KDC internal error";
349 return KRB5KRB_ERR_GENERIC
;
352 ret
= krb5_crypto_init(context
, skey
, etype
, &crypto
);
356 msg
= krb5_get_error_message(context
, ret
);
357 kdc_log(context
, config
, 0, "krb5_crypto_init failed: %s", msg
);
358 krb5_free_error_message(context
, msg
);
362 ret
= krb5_encrypt_EncryptedData(context
,
368 &rep
->ticket
.enc_part
);
370 krb5_crypto_destroy(context
, crypto
);
372 const char *msg
= krb5_get_error_message(context
, ret
);
373 kdc_log(context
, config
, 0, "Failed to encrypt data: %s", msg
);
374 krb5_free_error_message(context
, msg
);
378 if(rep
->msg_type
== krb_as_rep
&& !config
->encode_as_rep_as_tgs_rep
)
379 ASN1_MALLOC_ENCODE(EncASRepPart
, buf
, buf_size
, ek
, &len
, ret
);
381 ASN1_MALLOC_ENCODE(EncTGSRepPart
, buf
, buf_size
, ek
, &len
, ret
);
383 const char *msg
= krb5_get_error_message(context
, ret
);
384 kdc_log(context
, config
, 0, "Failed to encode KDC-REP: %s", msg
);
385 krb5_free_error_message(context
, msg
);
388 if(buf_size
!= len
) {
390 kdc_log(context
, config
, 0, "Internal error in ASN.1 encoder");
391 *e_text
= "KDC internal error";
392 return KRB5KRB_ERR_GENERIC
;
394 ret
= krb5_crypto_init(context
, reply_key
, 0, &crypto
);
396 const char *msg
= krb5_get_error_message(context
, ret
);
398 kdc_log(context
, config
, 0, "krb5_crypto_init failed: %s", msg
);
399 krb5_free_error_message(context
, msg
);
402 if(rep
->msg_type
== krb_as_rep
) {
403 krb5_encrypt_EncryptedData(context
,
405 KRB5_KU_AS_REP_ENC_PART
,
411 ASN1_MALLOC_ENCODE(AS_REP
, buf
, buf_size
, rep
, &len
, ret
);
413 krb5_encrypt_EncryptedData(context
,
415 rk_is_subkey
? KRB5_KU_TGS_REP_ENC_PART_SUB_KEY
: KRB5_KU_TGS_REP_ENC_PART_SESSION
,
421 ASN1_MALLOC_ENCODE(TGS_REP
, buf
, buf_size
, rep
, &len
, ret
);
423 krb5_crypto_destroy(context
, crypto
);
425 const char *msg
= krb5_get_error_message(context
, ret
);
426 kdc_log(context
, config
, 0, "Failed to encode KDC-REP: %s", msg
);
427 krb5_free_error_message(context
, msg
);
430 if(buf_size
!= len
) {
432 kdc_log(context
, config
, 0, "Internal error in ASN.1 encoder");
433 *e_text
= "KDC internal error";
434 return KRB5KRB_ERR_GENERIC
;
437 reply
->length
= buf_size
;
442 * Return 1 if the client have only older enctypes, this is for
443 * determining if the server should send ETYPE_INFO2 or not.
447 older_enctype(krb5_enctype enctype
)
450 case ETYPE_DES_CBC_CRC
:
451 case ETYPE_DES_CBC_MD4
:
452 case ETYPE_DES_CBC_MD5
:
453 case ETYPE_DES3_CBC_SHA1
:
454 case ETYPE_ARCFOUR_HMAC_MD5
:
455 case ETYPE_ARCFOUR_HMAC_MD5_56
:
457 * The following three is "old" windows enctypes and is needed for
458 * windows 2000 hosts.
460 case ETYPE_ARCFOUR_MD4
:
461 case ETYPE_ARCFOUR_HMAC_OLD
:
462 case ETYPE_ARCFOUR_HMAC_OLD_EXP
:
473 static krb5_error_code
474 make_etype_info_entry(krb5_context context
, ETYPE_INFO_ENTRY
*ent
, Key
*key
)
476 ent
->etype
= key
->key
.keytype
;
479 ALLOC(ent
->salttype
);
481 if(key
->salt
->type
== hdb_pw_salt
)
482 *ent
->salttype
= 0; /* or 1? or NULL? */
483 else if(key
->salt
->type
== hdb_afs3_salt
)
486 kdc_log(context
, config
, 0, "unknown salt-type: %d",
488 return KRB5KRB_ERR_GENERIC
;
490 /* according to `the specs', we can't send a salt if
491 we have AFS3 salted key, but that requires that you
492 *know* what cell you are using (e.g by assuming
493 that the cell is the same as the realm in lower
496 ALLOC(ent
->salttype
);
497 *ent
->salttype
= key
->salt
->type
;
500 * We shouldn't sent salttype since it is incompatible with the
501 * specification and it breaks windows clients. The afs
502 * salting problem is solved by using KRB5-PADATA-AFS3-SALT
503 * implemented in Heimdal 0.7 and later.
505 ent
->salttype
= NULL
;
507 krb5_copy_data(context
, &key
->salt
->salt
,
510 /* we return no salt type at all, as that should indicate
511 * the default salt type and make everybody happy. some
512 * systems (like w2k) dislike being told the salt type
515 ent
->salttype
= NULL
;
521 static krb5_error_code
522 get_pa_etype_info(krb5_context context
,
523 krb5_kdc_configuration
*config
,
524 METHOD_DATA
*md
, Key
*ckey
)
526 krb5_error_code ret
= 0;
533 pa
.val
= calloc(1, sizeof(pa
.val
[0]));
537 ret
= make_etype_info_entry(context
, &pa
.val
[0], ckey
);
539 free_ETYPE_INFO(&pa
);
543 ASN1_MALLOC_ENCODE(ETYPE_INFO
, buf
, len
, &pa
, &len
, ret
);
544 free_ETYPE_INFO(&pa
);
547 ret
= realloc_method_data(md
);
552 md
->val
[md
->len
- 1].padata_type
= KRB5_PADATA_ETYPE_INFO
;
553 md
->val
[md
->len
- 1].padata_value
.length
= len
;
554 md
->val
[md
->len
- 1].padata_value
.data
= buf
;
562 extern int _krb5_AES_string_to_default_iterator
;
564 static krb5_error_code
565 make_etype_info2_entry(ETYPE_INFO2_ENTRY
*ent
, Key
*key
)
567 ent
->etype
= key
->key
.keytype
;
570 if (ent
->salt
== NULL
)
572 *ent
->salt
= malloc(key
->salt
->salt
.length
+ 1);
573 if (*ent
->salt
== NULL
) {
578 memcpy(*ent
->salt
, key
->salt
->salt
.data
, key
->salt
->salt
.length
);
579 (*ent
->salt
)[key
->salt
->salt
.length
] = '\0';
583 ent
->s2kparams
= NULL
;
585 switch (key
->key
.keytype
) {
586 case ETYPE_AES128_CTS_HMAC_SHA1_96
:
587 case ETYPE_AES256_CTS_HMAC_SHA1_96
:
588 ALLOC(ent
->s2kparams
);
589 if (ent
->s2kparams
== NULL
)
591 ent
->s2kparams
->length
= 4;
592 ent
->s2kparams
->data
= malloc(ent
->s2kparams
->length
);
593 if (ent
->s2kparams
->data
== NULL
) {
594 free(ent
->s2kparams
);
595 ent
->s2kparams
= NULL
;
598 _krb5_put_int(ent
->s2kparams
->data
,
599 _krb5_AES_string_to_default_iterator
,
600 ent
->s2kparams
->length
);
602 case ETYPE_DES_CBC_CRC
:
603 case ETYPE_DES_CBC_MD4
:
604 case ETYPE_DES_CBC_MD5
:
605 /* Check if this was a AFS3 salted key */
606 if(key
->salt
&& key
->salt
->type
== hdb_afs3_salt
){
607 ALLOC(ent
->s2kparams
);
608 if (ent
->s2kparams
== NULL
)
610 ent
->s2kparams
->length
= 1;
611 ent
->s2kparams
->data
= malloc(ent
->s2kparams
->length
);
612 if (ent
->s2kparams
->data
== NULL
) {
613 free(ent
->s2kparams
);
614 ent
->s2kparams
= NULL
;
617 _krb5_put_int(ent
->s2kparams
->data
,
619 ent
->s2kparams
->length
);
629 * Return an ETYPE-INFO2. Enctypes are storted the same way as in the
630 * database (client supported enctypes first, then the unsupported
634 static krb5_error_code
635 get_pa_etype_info2(krb5_context context
,
636 krb5_kdc_configuration
*config
,
637 METHOD_DATA
*md
, Key
*ckey
)
639 krb5_error_code ret
= 0;
645 pa
.val
= calloc(1, sizeof(pa
.val
[0]));
649 ret
= make_etype_info2_entry(&pa
.val
[0], ckey
);
651 free_ETYPE_INFO2(&pa
);
655 ASN1_MALLOC_ENCODE(ETYPE_INFO2
, buf
, len
, &pa
, &len
, ret
);
656 free_ETYPE_INFO2(&pa
);
659 ret
= realloc_method_data(md
);
664 md
->val
[md
->len
- 1].padata_type
= KRB5_PADATA_ETYPE_INFO2
;
665 md
->val
[md
->len
- 1].padata_value
.length
= len
;
666 md
->val
[md
->len
- 1].padata_value
.data
= buf
;
675 log_as_req(krb5_context context
,
676 krb5_kdc_configuration
*config
,
679 const KDC_REQ_BODY
*b
)
682 struct rk_strpool
*p
;
686 p
= rk_strpoolprintf(NULL
, "%s", "Client supported enctypes: ");
688 for (i
= 0; i
< b
->etype
.len
; i
++) {
689 ret
= krb5_enctype_to_string(context
, b
->etype
.val
[i
], &str
);
691 p
= rk_strpoolprintf(p
, "%s", str
);
694 p
= rk_strpoolprintf(p
, "%d", b
->etype
.val
[i
]);
695 if (p
&& i
+ 1 < b
->etype
.len
)
696 p
= rk_strpoolprintf(p
, ", ");
698 kdc_log(context
, config
, 0, "out of memory");
703 p
= rk_strpoolprintf(p
, "no encryption types");
709 ret
= krb5_enctype_to_string(context
, cetype
, &cet
);
711 ret
= krb5_enctype_to_string(context
, setype
, &set
);
713 p
= rk_strpoolprintf(p
, ", using %s/%s", cet
, set
);
719 p
= rk_strpoolprintf(p
, ", using enctypes %d/%d",
723 str
= rk_strpoolcollect(p
);
724 kdc_log(context
, config
, 0, "%s", str
);
729 unparse_flags(KDCOptions2int(b
->kdc_options
), asn1_KDCOptions_units(),
730 fixedstr
, sizeof(fixedstr
));
732 kdc_log(context
, config
, 0, "Requested flags: %s", fixedstr
);
737 * verify the flags on `client' and `server', returning 0
738 * if they are OK and generating an error messages and returning
739 * and error code otherwise.
743 kdc_check_flags(krb5_context context
,
744 krb5_kdc_configuration
*config
,
745 hdb_entry_ex
*client_ex
, const char *client_name
,
746 hdb_entry_ex
*server_ex
, const char *server_name
,
747 krb5_boolean is_as_req
)
749 if(client_ex
!= NULL
) {
750 hdb_entry
*client
= &client_ex
->entry
;
753 if (client
->flags
.locked_out
) {
754 kdc_log(context
, config
, 0,
755 "Client (%s) is locked out", client_name
);
756 return KRB5KDC_ERR_CLIENT_REVOKED
;
759 if (client
->flags
.invalid
) {
760 kdc_log(context
, config
, 0,
761 "Client (%s) has invalid bit set", client_name
);
762 return KRB5KDC_ERR_POLICY
;
765 if (!is_as_req
&& !client
->flags
.client
){
766 kdc_log(context
, config
, 0,
767 "Principal may only act as client in AS-REQ -- %s", client_name
);
768 return KRB5KDC_ERR_POLICY
;
771 if (client
->valid_start
&& *client
->valid_start
> kdc_time
) {
772 char starttime_str
[100];
773 krb5_format_time(context
, *client
->valid_start
,
774 starttime_str
, sizeof(starttime_str
), TRUE
);
775 kdc_log(context
, config
, 0,
776 "Client not yet valid until %s -- %s",
777 starttime_str
, client_name
);
778 return KRB5KDC_ERR_CLIENT_NOTYET
;
781 if (client
->valid_end
&& *client
->valid_end
< kdc_time
) {
782 char endtime_str
[100];
783 krb5_format_time(context
, *client
->valid_end
,
784 endtime_str
, sizeof(endtime_str
), TRUE
);
785 kdc_log(context
, config
, 0,
786 "Client expired at %s -- %s",
787 endtime_str
, client_name
);
788 return KRB5KDC_ERR_NAME_EXP
;
791 if (client
->pw_end
&& *client
->pw_end
< kdc_time
792 && (server_ex
== NULL
|| !server_ex
->entry
.flags
.change_pw
)) {
794 krb5_format_time(context
, *client
->pw_end
,
795 pwend_str
, sizeof(pwend_str
), TRUE
);
796 kdc_log(context
, config
, 0,
797 "Client's key has expired at %s -- %s",
798 pwend_str
, client_name
);
799 return KRB5KDC_ERR_KEY_EXPIRED
;
805 if (server_ex
!= NULL
) {
806 hdb_entry
*server
= &server_ex
->entry
;
808 if (server
->flags
.locked_out
) {
809 kdc_log(context
, config
, 0,
810 "Client server locked out -- %s", server_name
);
811 return KRB5KDC_ERR_POLICY
;
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
);
941 _kdc_is_anonymous(krb5_context context
, krb5_principal principal
)
943 if (principal
->name
.name_type
!= KRB5_NT_WELLKNOWN
||
944 principal
->name
.name_string
.len
!= 2 ||
945 strcmp(principal
->name
.name_string
.val
[0], KRB5_WELLKNOWN_NAME
) != 0 ||
946 strcmp(principal
->name
.name_string
.val
[1], KRB5_ANON_NAME
) != 0)
956 _kdc_as_rep(krb5_context context
,
957 krb5_kdc_configuration
*config
,
959 const krb5_data
*req_buffer
,
962 struct sockaddr
*from_addr
,
965 KDC_REQ_BODY
*b
= &req
->req_body
;
967 KDCOptions f
= b
->kdc_options
;
968 hdb_entry_ex
*client
= NULL
, *server
= NULL
;
969 HDB
*clientdb
= NULL
;
970 krb5_enctype setype
, sessionetype
;
974 krb5_principal client_princ
= NULL
, server_princ
= NULL
;
975 char *client_name
= NULL
, *server_name
= NULL
;
976 krb5_error_code ret
= 0;
977 const char *e_text
= NULL
;
980 EncryptionKey
*reply_key
= NULL
, session_key
;
981 int flags
= HDB_F_FOR_AS_REQ
;
983 pk_client_params
*pkp
= NULL
;
985 const EncryptionKey
*pk_reply_key
= NULL
;
987 memset(&rep
, 0, sizeof(rep
));
988 memset(&session_key
, 0, sizeof(session_key
));
989 krb5_data_zero(&e_data
);
993 rep
.padata
->val
= NULL
;
996 flags
|= HDB_F_CANON
;
998 if(b
->sname
== NULL
){
999 ret
= KRB5KRB_ERR_GENERIC
;
1000 e_text
= "No server in request";
1002 ret
= _krb5_principalname2krb5_principal (context
,
1007 ret
= krb5_unparse_name(context
, server_princ
, &server_name
);
1010 kdc_log(context
, config
, 0,
1011 "AS-REQ malformed server name from %s", from
);
1014 if(b
->cname
== NULL
){
1015 ret
= KRB5KRB_ERR_GENERIC
;
1016 e_text
= "No client in request";
1018 ret
= _krb5_principalname2krb5_principal (context
,
1025 ret
= krb5_unparse_name(context
, client_princ
, &client_name
);
1028 kdc_log(context
, config
, 0,
1029 "AS-REQ malformed client name from %s", from
);
1033 kdc_log(context
, config
, 0, "AS-REQ %s from %s for %s",
1034 client_name
, from
, server_name
);
1040 if (_kdc_is_anonymous(context
, client_princ
)) {
1041 if (!b
->kdc_options
.request_anonymous
) {
1042 kdc_log(context
, config
, 0, "Anonymous ticket w/o anonymous flag");
1043 ret
= KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN
;
1046 } else if (b
->kdc_options
.request_anonymous
) {
1047 kdc_log(context
, config
, 0,
1048 "Request for a anonymous ticket with non "
1049 "anonymous client name: %s", client_name
);
1050 ret
= KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN
;
1058 ret
= _kdc_db_fetch(context
, config
, client_princ
,
1059 HDB_F_GET_ANY
| flags
, NULL
,
1060 &clientdb
, &client
);
1061 if(ret
== HDB_ERR_NOT_FOUND_HERE
) {
1062 kdc_log(context
, config
, 5, "client %s does not have secrets at this KDC, need to proxy", client_name
);
1064 } else if (ret
== HDB_ERR_WRONG_REALM
) {
1065 char *fixed_client_name
= NULL
;
1067 ret
= krb5_unparse_name(context
, client
->entry
.principal
,
1068 &fixed_client_name
);
1073 kdc_log(context
, config
, 0, "WRONG_REALM - %s -> %s",
1074 client_name
, fixed_client_name
);
1075 free(fixed_client_name
);
1077 ret
= krb5_mk_error_ext(context
,
1078 KRB5_KDC_ERR_WRONG_REALM
,
1082 NULL
, /* client_name */
1083 &client
->entry
.principal
->realm
,
1084 NULL
, /* client_time */
1085 NULL
, /* client_usec */
1089 const char *msg
= krb5_get_error_message(context
, ret
);
1090 kdc_log(context
, config
, 0, "UNKNOWN -- %s: %s", client_name
, msg
);
1091 krb5_free_error_message(context
, msg
);
1092 ret
= KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN
;
1094 if (config
->db
[0] && config
->db
[0]->hdb_auth_status
)
1095 (config
->db
[0]->hdb_auth_status
)(context
, config
->db
[0], NULL
,
1099 HDB_AUTH_CLIENT_UNKNOWN
);
1102 ret
= _kdc_db_fetch(context
, config
, server_princ
,
1103 HDB_F_GET_SERVER
|HDB_F_GET_KRBTGT
| flags
,
1104 NULL
, NULL
, &server
);
1105 if(ret
== HDB_ERR_NOT_FOUND_HERE
) {
1106 kdc_log(context
, config
, 5, "target %s does not have secrets at this KDC, need to proxy", server_name
);
1109 const char *msg
= krb5_get_error_message(context
, ret
);
1110 kdc_log(context
, config
, 0, "UNKNOWN -- %s: %s", server_name
, msg
);
1111 krb5_free_error_message(context
, msg
);
1112 ret
= KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN
;
1116 memset(&et
, 0, sizeof(et
));
1117 memset(&ek
, 0, sizeof(ek
));
1120 * Select a session enctype from the list of the crypto system
1121 * supported enctypes that is supported by the client and is one of
1122 * the enctype of the enctype of the service (likely krbtgt).
1124 * The latter is used as a hint of what enctypes all KDC support,
1125 * to make sure a newer version of KDC won't generate a session
1126 * enctype that an older version of a KDC in the same realm can't
1129 ret
= _kdc_find_etype(context
, config
->as_use_strongest_session_key
, FALSE
,
1130 client
, b
->etype
.val
, b
->etype
.len
, &sessionetype
,
1133 kdc_log(context
, config
, 0,
1134 "Client (%s) from %s has no common enctypes with KDC "
1135 "to use for the session key",
1140 * But if the KDC admin is paranoid and doesn't want to have "not
1141 * the best" enctypes on the krbtgt, lets save the best pick from
1142 * the client list and hope that that will work for any other
1147 * Pre-auth processing
1155 log_patypes(context
, config
, req
->padata
);
1158 kdc_log(context
, config
, 5,
1159 "Looking for PKINIT pa-data -- %s", client_name
);
1161 e_text
= "No PKINIT PA found";
1164 pa
= _kdc_find_padata(req
, &i
, KRB5_PADATA_PK_AS_REQ
);
1167 pa
= _kdc_find_padata(req
, &i
, KRB5_PADATA_PK_AS_REQ_WIN
);
1170 char *client_cert
= NULL
;
1172 ret
= _kdc_pk_rd_padata(context
, config
, req
, pa
, client
, &pkp
);
1174 ret
= KRB5KRB_AP_ERR_BAD_INTEGRITY
;
1175 kdc_log(context
, config
, 5,
1176 "Failed to decode PKINIT PA-DATA -- %s",
1180 if (ret
== 0 && pkp
== NULL
)
1183 ret
= _kdc_pk_check_client(context
,
1190 e_text
= "PKINIT certificate not allowed to "
1191 "impersonate principal";
1192 _kdc_pk_free_client_param(context
, pkp
);
1194 kdc_log(context
, config
, 0, "%s", e_text
);
1200 et
.flags
.pre_authent
= 1;
1201 kdc_log(context
, config
, 0,
1202 "PKINIT pre-authentication succeeded -- %s using %s",
1203 client_name
, client_cert
);
1204 if (clientdb
->hdb_auth_status
)
1205 (clientdb
->hdb_auth_status
)(context
, clientdb
, client
,
1209 HDB_AUTH_PKINIT_SUCCESS
);
1217 if (client
->entry
.flags
.locked_out
) {
1218 ret
= KRB5KDC_ERR_CLIENT_REVOKED
;
1219 kdc_log(context
, config
, 0,
1220 "Client (%s) is locked out", client_name
);
1224 kdc_log(context
, config
, 5, "Looking for ENC-TS pa-data -- %s",
1228 e_text
= "No ENC-TS found";
1229 while((pa
= _kdc_find_padata(req
, &i
, KRB5_PADATA_ENC_TIMESTAMP
))){
1233 EncryptedData enc_data
;
1239 if (b
->kdc_options
.request_anonymous
) {
1240 ret
= KRB5KRB_AP_ERR_BAD_INTEGRITY
;
1241 kdc_log(context
, config
, 0, "ENC-TS doesn't support anon");
1245 ret
= decode_EncryptedData(pa
->padata_value
.data
,
1246 pa
->padata_value
.length
,
1250 ret
= KRB5KRB_AP_ERR_BAD_INTEGRITY
;
1251 kdc_log(context
, config
, 5, "Failed to decode PA-DATA -- %s",
1256 ret
= hdb_enctype2key(context
, &client
->entry
,
1257 enc_data
.etype
, &pa_key
);
1260 e_text
= "No key matches pa-data";
1261 ret
= KRB5KDC_ERR_ETYPE_NOSUPP
;
1262 if(krb5_enctype_to_string(context
, enc_data
.etype
, &estr
))
1265 kdc_log(context
, config
, 5,
1266 "No client key matching pa-data (%d) -- %s",
1267 enc_data
.etype
, client_name
);
1269 kdc_log(context
, config
, 5,
1270 "No client key matching pa-data (%s) -- %s",
1273 free_EncryptedData(&enc_data
);
1279 ret
= krb5_crypto_init(context
, &pa_key
->key
, 0, &crypto
);
1281 const char *msg
= krb5_get_error_message(context
, ret
);
1282 kdc_log(context
, config
, 0, "krb5_crypto_init failed: %s", msg
);
1283 krb5_free_error_message(context
, msg
);
1284 free_EncryptedData(&enc_data
);
1288 ret
= krb5_decrypt_EncryptedData (context
,
1290 KRB5_KU_PA_ENC_TIMESTAMP
,
1293 krb5_crypto_destroy(context
, crypto
);
1295 * Since the user might have several keys with the same
1296 * enctype but with diffrent salting, we need to try all
1297 * the keys with the same enctype.
1300 krb5_error_code ret2
;
1301 const char *msg
= krb5_get_error_message(context
, ret
);
1303 ret2
= krb5_enctype_to_string(context
,
1304 pa_key
->key
.keytype
, &str
);
1308 kdc_log(context
, config
, 5,
1309 "Failed to decrypt PA-DATA -- %s "
1310 "(enctype %s) error %s",
1311 client_name
, str
? str
: "unknown enctype", msg
);
1312 krb5_free_error_message(context
, msg
);
1314 if(hdb_next_enctype2key(context
, &client
->entry
,
1315 enc_data
.etype
, &pa_key
) == 0) {
1319 e_text
= "Failed to decrypt PA-DATA";
1321 free_EncryptedData(&enc_data
);
1323 if (clientdb
->hdb_auth_status
)
1324 (clientdb
->hdb_auth_status
)(context
, clientdb
, client
,
1327 str
? str
: "unknown enctype",
1328 HDB_AUTH_WRONG_PASSWORD
);
1332 ret
= KRB5KDC_ERR_PREAUTH_FAILED
;
1335 free_EncryptedData(&enc_data
);
1336 ret
= decode_PA_ENC_TS_ENC(ts_data
.data
,
1340 krb5_data_free(&ts_data
);
1342 e_text
= "Failed to decode PA-ENC-TS-ENC";
1343 ret
= KRB5KDC_ERR_PREAUTH_FAILED
;
1344 kdc_log(context
, config
,
1345 5, "Failed to decode PA-ENC-TS_ENC -- %s",
1349 free_PA_ENC_TS_ENC(&p
);
1350 if (abs(kdc_time
- p
.patimestamp
) > context
->max_skew
) {
1351 char client_time
[100];
1353 krb5_format_time(context
, p
.patimestamp
,
1354 client_time
, sizeof(client_time
), TRUE
);
1356 ret
= KRB5KRB_AP_ERR_SKEW
;
1357 kdc_log(context
, config
, 0,
1358 "Too large time skew, "
1359 "client time %s is out by %u > %u seconds -- %s",
1361 (unsigned)abs(kdc_time
- p
.patimestamp
),
1366 * The following is needed to make windows clients to
1367 * retry using the timestamp in the error message, if
1368 * there is a e_text, they become unhappy.
1373 et
.flags
.pre_authent
= 1;
1375 set_salt_padata(rep
.padata
, pa_key
->salt
);
1377 reply_key
= &pa_key
->key
;
1379 ret
= krb5_enctype_to_string(context
, pa_key
->key
.keytype
, &str
);
1383 kdc_log(context
, config
, 2,
1384 "ENC-TS Pre-authentication succeeded -- %s using %s",
1385 client_name
, str
? str
: "unknown enctype");
1386 if (clientdb
->hdb_auth_status
)
1387 (clientdb
->hdb_auth_status
)(context
, clientdb
, client
,
1390 str
? str
: "unknown enctype",
1391 HDB_AUTH_CORRECT_PASSWORD
);
1399 if(found_pa
== 0 && config
->require_preauth
)
1401 /* We come here if we found a pa-enc-timestamp, but if there
1402 was some problem with it, other than too large skew */
1403 if(found_pa
&& et
.flags
.pre_authent
== 0){
1404 kdc_log(context
, config
, 0, "%s -- %s", e_text
, client_name
);
1405 if (!prepare_enc_data(context
, config
, &e_data
, b
, client
)) {
1411 }else if (config
->require_preauth
1412 || b
->kdc_options
.request_anonymous
/* hack to force anon */
1413 || client
->entry
.flags
.require_preauth
1414 || server
->entry
.flags
.require_preauth
) {
1416 if (!prepare_enc_data(context
, config
, &e_data
, b
, client
)) {
1420 e_text
="Need to use PA-ENC-TIMESTAMP/PA-PK-AS-REQ",
1422 ret
= KRB5KDC_ERR_PREAUTH_REQUIRED
;
1424 kdc_log(context
, config
, 0,
1425 "No preauth found, returning PREAUTH-REQUIRED -- %s",
1433 * Verify flags after the user been required to prove its identity
1434 * with in a preauth mech.
1437 ret
= _kdc_check_access(context
, config
, client
, client_name
,
1438 server
, server_name
,
1443 if (clientdb
->hdb_auth_status
)
1444 (clientdb
->hdb_auth_status
)(context
, clientdb
, client
,
1451 * Selelct the best encryption type for the KDC with out regard to
1452 * the client since the client never needs to read that data.
1455 ret
= _kdc_get_preferred_key(context
, config
,
1456 server
, server_name
,
1461 if(f
.renew
|| f
.validate
|| f
.proxy
|| f
.forwarded
|| f
.enc_tkt_in_skey
1462 || (f
.request_anonymous
&& !config
->allow_anonymous
)) {
1463 ret
= KRB5KDC_ERR_BADOPTION
;
1464 e_text
= "Bad KDC options";
1465 kdc_log(context
, config
, 0, "Bad KDC options -- %s", client_name
);
1470 rep
.msg_type
= krb_as_rep
;
1472 ret
= copy_Realm(&client
->entry
.principal
->realm
, &rep
.crealm
);
1475 ret
= _krb5_principal2principalname(&rep
.cname
, client
->entry
.principal
);
1479 rep
.ticket
.tkt_vno
= 5;
1480 copy_Realm(&server
->entry
.principal
->realm
, &rep
.ticket
.realm
);
1481 _krb5_principal2principalname(&rep
.ticket
.sname
,
1482 server
->entry
.principal
);
1483 /* java 1.6 expects the name to be the same type, lets allow that
1484 * uncomplicated name-types. */
1485 #define CNT(sp,t) (((sp)->sname->name_type) == KRB5_NT_##t)
1486 if (CNT(b
, UNKNOWN
) || CNT(b
, PRINCIPAL
) || CNT(b
, SRV_INST
) || CNT(b
, SRV_HST
) || CNT(b
, SRV_XHST
))
1487 rep
.ticket
.sname
.name_type
= b
->sname
->name_type
;
1490 et
.flags
.initial
= 1;
1491 if(client
->entry
.flags
.forwardable
&& server
->entry
.flags
.forwardable
)
1492 et
.flags
.forwardable
= f
.forwardable
;
1493 else if (f
.forwardable
) {
1494 e_text
= "Ticket may not be forwardable";
1495 ret
= KRB5KDC_ERR_POLICY
;
1496 kdc_log(context
, config
, 0,
1497 "Ticket may not be forwardable -- %s", client_name
);
1500 if(client
->entry
.flags
.proxiable
&& server
->entry
.flags
.proxiable
)
1501 et
.flags
.proxiable
= f
.proxiable
;
1502 else if (f
.proxiable
) {
1503 e_text
= "Ticket may not be proxiable";
1504 ret
= KRB5KDC_ERR_POLICY
;
1505 kdc_log(context
, config
, 0,
1506 "Ticket may not be proxiable -- %s", client_name
);
1509 if(client
->entry
.flags
.postdate
&& server
->entry
.flags
.postdate
)
1510 et
.flags
.may_postdate
= f
.allow_postdate
;
1511 else if (f
.allow_postdate
){
1512 e_text
= "Ticket may not be postdate";
1513 ret
= KRB5KDC_ERR_POLICY
;
1514 kdc_log(context
, config
, 0,
1515 "Ticket may not be postdatable -- %s", client_name
);
1519 /* check for valid set of addresses */
1520 if(!_kdc_check_addresses(context
, config
, b
->addresses
, from_addr
)) {
1521 e_text
= "Bad address list in requested";
1522 ret
= KRB5KRB_AP_ERR_BADADDR
;
1523 kdc_log(context
, config
, 0,
1524 "Bad address list requested -- %s", client_name
);
1528 ret
= copy_PrincipalName(&rep
.cname
, &et
.cname
);
1531 ret
= copy_Realm(&rep
.crealm
, &et
.crealm
);
1539 start
= et
.authtime
= kdc_time
;
1541 if(f
.postdated
&& req
->req_body
.from
){
1542 ALLOC(et
.starttime
);
1543 start
= *et
.starttime
= *req
->req_body
.from
;
1544 et
.flags
.invalid
= 1;
1545 et
.flags
.postdated
= 1; /* XXX ??? */
1547 _kdc_fix_time(&b
->till
);
1550 /* be careful not overflowing */
1552 if(client
->entry
.max_life
)
1553 t
= start
+ min(t
- start
, *client
->entry
.max_life
);
1554 if(server
->entry
.max_life
)
1555 t
= start
+ min(t
- start
, *server
->entry
.max_life
);
1557 t
= min(t
, start
+ realm
->max_life
);
1560 if(f
.renewable_ok
&& et
.endtime
< *b
->till
){
1562 if(b
->rtime
== NULL
){
1566 if(*b
->rtime
< *b
->till
)
1567 *b
->rtime
= *b
->till
;
1569 if(f
.renewable
&& b
->rtime
){
1573 if(client
->entry
.max_renew
)
1574 t
= start
+ min(t
- start
, *client
->entry
.max_renew
);
1575 if(server
->entry
.max_renew
)
1576 t
= start
+ min(t
- start
, *server
->entry
.max_renew
);
1578 t
= min(t
, start
+ realm
->max_renew
);
1580 ALLOC(et
.renew_till
);
1582 et
.flags
.renewable
= 1;
1586 if (f
.request_anonymous
)
1587 et
.flags
.anonymous
= 1;
1591 copy_HostAddresses(b
->addresses
, et
.caddr
);
1594 et
.transited
.tr_type
= DOMAIN_X500_COMPRESS
;
1595 krb5_data_zero(&et
.transited
.contents
);
1597 /* The MIT ASN.1 library (obviously) doesn't tell lengths encoded
1598 * as 0 and as 0x80 (meaning indefinite length) apart, and is thus
1599 * incapable of correctly decoding SEQUENCE OF's of zero length.
1601 * To fix this, always send at least one no-op last_req
1603 * If there's a pw_end or valid_end we will use that,
1604 * otherwise just a dummy lr.
1606 ek
.last_req
.val
= malloc(2 * sizeof(*ek
.last_req
.val
));
1607 if (ek
.last_req
.val
== NULL
) {
1611 ek
.last_req
.len
= 0;
1612 if (client
->entry
.pw_end
1613 && (config
->kdc_warn_pwexpire
== 0
1614 || kdc_time
+ config
->kdc_warn_pwexpire
>= *client
->entry
.pw_end
)) {
1615 ek
.last_req
.val
[ek
.last_req
.len
].lr_type
= LR_PW_EXPTIME
;
1616 ek
.last_req
.val
[ek
.last_req
.len
].lr_value
= *client
->entry
.pw_end
;
1619 if (client
->entry
.valid_end
) {
1620 ek
.last_req
.val
[ek
.last_req
.len
].lr_type
= LR_ACCT_EXPTIME
;
1621 ek
.last_req
.val
[ek
.last_req
.len
].lr_value
= *client
->entry
.valid_end
;
1624 if (ek
.last_req
.len
== 0) {
1625 ek
.last_req
.val
[ek
.last_req
.len
].lr_type
= LR_NONE
;
1626 ek
.last_req
.val
[ek
.last_req
.len
].lr_value
= 0;
1629 ek
.nonce
= b
->nonce
;
1630 if (client
->entry
.valid_end
|| client
->entry
.pw_end
) {
1631 ALLOC(ek
.key_expiration
);
1632 if (client
->entry
.valid_end
) {
1633 if (client
->entry
.pw_end
)
1634 *ek
.key_expiration
= min(*client
->entry
.valid_end
,
1635 *client
->entry
.pw_end
);
1637 *ek
.key_expiration
= *client
->entry
.valid_end
;
1639 *ek
.key_expiration
= *client
->entry
.pw_end
;
1641 ek
.key_expiration
= NULL
;
1642 ek
.flags
= et
.flags
;
1643 ek
.authtime
= et
.authtime
;
1645 ALLOC(ek
.starttime
);
1646 *ek
.starttime
= *et
.starttime
;
1648 ek
.endtime
= et
.endtime
;
1649 if (et
.renew_till
) {
1650 ALLOC(ek
.renew_till
);
1651 *ek
.renew_till
= *et
.renew_till
;
1653 copy_Realm(&rep
.ticket
.realm
, &ek
.srealm
);
1654 copy_PrincipalName(&rep
.ticket
.sname
, &ek
.sname
);
1657 copy_HostAddresses(et
.caddr
, ek
.caddr
);
1662 e_text
= "Failed to build PK-INIT reply";
1663 ret
= _kdc_pk_mk_pa_reply(context
, config
, pkp
, client
,
1664 sessionetype
, req
, req_buffer
,
1665 &reply_key
, &et
.key
, rep
.padata
);
1668 ret
= _kdc_add_inital_verified_cas(context
,
1676 * Send reply key as constant value to pac generate which allows
1677 * parts of the buffer to be encrypted (i.e., PAC_CREDENTIAL_DATA).
1679 pk_reply_key
= reply_key
;
1683 ret
= krb5_generate_random_keyblock(context
, sessionetype
, &et
.key
);
1688 if (reply_key
== NULL
) {
1689 e_text
= "Client have no reply key";
1690 ret
= KRB5KDC_ERR_CLIENT_NOTYET
;
1694 ret
= copy_EncryptionKey(&et
.key
, &ek
.key
);
1698 if (rep
.padata
->len
== 0) {
1704 if (send_pac_p(context
, req
)) {
1708 ret
= _kdc_pac_generate(context
, client
, pk_reply_key
, &p
);
1710 kdc_log(context
, config
, 0, "PAC generation failed for -- %s",
1715 ret
= _krb5_pac_sign(context
, p
, et
.authtime
,
1716 client
->entry
.principal
,
1717 &skey
->key
, /* Server key */
1718 &skey
->key
, /* FIXME: should be krbtgt key */
1720 krb5_pac_free(context
, p
);
1722 kdc_log(context
, config
, 0, "PAC signing failed for -- %s",
1727 ret
= _kdc_tkt_add_if_relevant_ad(context
, &et
,
1728 KRB5_AUTHDATA_WIN2K_PAC
,
1730 krb5_data_free(&data
);
1736 _kdc_log_timestamp(context
, config
, "AS-REQ", et
.authtime
, et
.starttime
,
1737 et
.endtime
, et
.renew_till
);
1739 /* do this as the last thing since this signs the EncTicketPart */
1740 ret
= _kdc_add_KRB5SignedPath(context
,
1744 client
->entry
.principal
,
1751 log_as_req(context
, config
, reply_key
->keytype
, setype
, b
);
1753 ret
= _kdc_encode_reply(context
, config
,
1754 &rep
, &et
, &ek
, setype
, server
->entry
.kvno
,
1755 &skey
->key
, client
->entry
.kvno
,
1756 reply_key
, 0, &e_text
, reply
);
1757 free_EncTicketPart(&et
);
1758 free_EncKDCRepPart(&ek
);
1763 if (datagram_reply
&& reply
->length
> config
->max_datagram_reply_length
) {
1764 krb5_data_free(reply
);
1765 ret
= KRB5KRB_ERR_RESPONSE_TOO_BIG
;
1766 e_text
= "Reply packet too large";
1771 if(ret
!= 0 && ret
!= HDB_ERR_NOT_FOUND_HERE
&& reply
->length
== 0) {
1772 krb5_mk_error(context
,
1775 (e_data
.data
? &e_data
: NULL
),
1785 _kdc_pk_free_client_param(context
, pkp
);
1790 krb5_free_principal(context
, client_princ
);
1793 krb5_free_principal(context
, server_princ
);
1796 _kdc_free_ent(context
, client
);
1798 _kdc_free_ent(context
, server
);
1803 prepare_enc_data(krb5_context context
,
1804 krb5_kdc_configuration
*config
,
1807 hdb_entry_ex
*client
)
1809 METHOD_DATA method_data
;
1814 krb5_error_code ret
;
1816 method_data
.len
= 0;
1817 method_data
.val
= NULL
;
1819 ret
= realloc_method_data(&method_data
);
1821 free_METHOD_DATA(&method_data
);
1824 pa
= &method_data
.val
[method_data
.len
-1];
1825 pa
->padata_type
= KRB5_PADATA_ENC_TIMESTAMP
;
1826 pa
->padata_value
.length
= 0;
1827 pa
->padata_value
.data
= NULL
;
1830 ret
= realloc_method_data(&method_data
);
1832 free_METHOD_DATA(&method_data
);
1835 pa
= &method_data
.val
[method_data
.len
-1];
1836 pa
->padata_type
= KRB5_PADATA_PK_AS_REQ
;
1837 pa
->padata_value
.length
= 0;
1838 pa
->padata_value
.data
= NULL
;
1840 ret
= realloc_method_data(&method_data
);
1842 free_METHOD_DATA(&method_data
);
1845 pa
= &method_data
.val
[method_data
.len
-1];
1846 pa
->padata_type
= KRB5_PADATA_PK_AS_REQ_WIN
;
1847 pa
->padata_value
.length
= 0;
1848 pa
->padata_value
.data
= NULL
;
1852 * If there is a client key, send ETYPE_INFO{,2}
1854 ret
= _kdc_find_etype(context
,
1855 config
->preauth_use_strongest_session_key
, TRUE
,
1856 client
, b
->etype
.val
, b
->etype
.len
, NULL
, &ckey
);
1861 * - If the client only knows about old enctypes, then send
1862 * both info replies (we send 'info' first in the list).
1863 * - If the client is 'modern', because it knows about 'new'
1864 * enctype types, then only send the 'info2' reply.
1866 * Before we send the full list of etype-info data, we pick
1867 * the client key we would have used anyway below, just pick
1871 if (older_enctype(ckey
->key
.keytype
)) {
1872 ret
= get_pa_etype_info(context
, config
,
1873 &method_data
, ckey
);
1875 free_METHOD_DATA(&method_data
);
1879 ret
= get_pa_etype_info2(context
, config
,
1880 &method_data
, ckey
);
1882 free_METHOD_DATA(&method_data
);
1887 ASN1_MALLOC_ENCODE(METHOD_DATA
, buf
, len
, &method_data
, &len
, ret
);
1888 free_METHOD_DATA(&method_data
);
1891 e_data
->length
= len
;
1897 * Add the AuthorizationData `data´ of `type´ to the last element in
1898 * the sequence of authorization_data in `tkt´ wrapped in an IF_RELEVANT
1902 _kdc_tkt_add_if_relevant_ad(krb5_context context
,
1905 const krb5_data
*data
)
1907 krb5_error_code ret
;
1910 if (tkt
->authorization_data
== NULL
) {
1911 tkt
->authorization_data
= calloc(1, sizeof(*tkt
->authorization_data
));
1912 if (tkt
->authorization_data
== NULL
) {
1913 krb5_set_error_message(context
, ENOMEM
, "out of memory");
1918 /* add the entry to the last element */
1920 AuthorizationData ad
= { 0, NULL
};
1921 AuthorizationDataElement ade
;
1924 ade
.ad_data
= *data
;
1926 ret
= add_AuthorizationData(&ad
, &ade
);
1928 krb5_set_error_message(context
, ret
, "add AuthorizationData failed");
1932 ade
.ad_type
= KRB5_AUTHDATA_IF_RELEVANT
;
1934 ASN1_MALLOC_ENCODE(AuthorizationData
,
1935 ade
.ad_data
.data
, ade
.ad_data
.length
,
1937 free_AuthorizationData(&ad
);
1939 krb5_set_error_message(context
, ret
, "ASN.1 encode of "
1940 "AuthorizationData failed");
1943 if (ade
.ad_data
.length
!= size
)
1944 krb5_abortx(context
, "internal asn.1 encoder error");
1946 ret
= add_AuthorizationData(tkt
->authorization_data
, &ade
);
1947 der_free_octet_string(&ade
.ad_data
);
1949 krb5_set_error_message(context
, ret
, "add AuthorizationData failed");