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
== (krb5_enctype
)ETYPE_DES_CBC_CRC
96 || etype
== (krb5_enctype
)ETYPE_DES_CBC_MD4
97 || etype
== (krb5_enctype
)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
= (krb5_enctype
)ETYPE_NULL
;
134 const krb5_enctype
*p
;
138 /* We'll want to avoid keys with v4 salted keys in the pre-auth case... */
139 ret
= krb5_get_pw_salt(context
, princ
->entry
.principal
, &def_salt
);
143 ret
= KRB5KDC_ERR_ETYPE_NOSUPP
;
145 if (use_strongest_session_key
) {
148 * Pick the strongest key that the KDC, target service, and
149 * client all support, using the local cryptosystem enctype
150 * list in strongest-to-weakest order to drive the search.
152 * This is not what RFC4120 says to do, but it encourages
153 * adoption of stronger enctypes. This doesn't play well with
154 * clients that have multiple Kerberos client implementations
155 * available with different supported enctype lists.
158 /* drive the search with local supported enctypes list */
159 p
= krb5_kerberos_enctypes(context
);
161 p
[i
] != (krb5_enctype
)ETYPE_NULL
&& enctype
== (krb5_enctype
)ETYPE_NULL
;
163 if (krb5_enctype_valid(context
, p
[i
]) != 0 &&
164 !_kdc_is_weak_exception(princ
->entry
.principal
, p
[i
]))
167 /* check that the client supports it too */
168 for (k
= 0; k
< len
&& enctype
== (krb5_enctype
)ETYPE_NULL
; k
++) {
170 if (p
[i
] != etypes
[k
])
173 /* check target princ support */
175 while (hdb_next_enctype2key(context
, &princ
->entry
, NULL
,
177 if (key
->key
.keyvalue
.length
== 0) {
178 ret
= KRB5KDC_ERR_NULL_KEY
;
183 if (is_preauth
&& ret_key
!= NULL
&&
184 !is_default_salt_p(&def_salt
, key
))
191 * Pick the first key from the client's enctype list that is
192 * supported by the cryptosystem and by the given principal.
194 * RFC4120 says we SHOULD pick the first _strong_ key from the
195 * client's list... not the first key... If the admin disallows
196 * weak enctypes in krb5.conf and selects this key selection
197 * algorithm, then we get exactly what RFC4120 says.
199 for(i
= 0; ret
!= 0 && i
< len
; i
++) {
201 if (krb5_enctype_valid(context
, etypes
[i
]) != 0 &&
202 !_kdc_is_weak_exception(princ
->entry
.principal
, etypes
[i
]))
207 hdb_next_enctype2key(context
, &princ
->entry
, NULL
,
208 etypes
[i
], &key
) == 0) {
209 if (key
->key
.keyvalue
.length
== 0) {
210 ret
= KRB5KDC_ERR_NULL_KEY
;
215 if (is_preauth
&& ret_key
!= NULL
&&
216 !is_default_salt_p(&def_salt
, key
))
222 if (enctype
== (krb5_enctype
)ETYPE_NULL
) {
224 * if the service principal is one for which there is a known 1DES
225 * exception and no other enctype matches both the client request and
226 * the service key list, provide a DES-CBC-CRC key.
228 if (ret_key
== NULL
&&
229 _kdc_is_weak_exception(princ
->entry
.principal
, ETYPE_DES_CBC_CRC
)) {
231 enctype
= ETYPE_DES_CBC_CRC
;
233 ret
= KRB5KDC_ERR_ETYPE_NOSUPP
;
238 if (ret_enctype
!= NULL
)
239 *ret_enctype
= enctype
;
244 krb5_free_salt (context
, def_salt
);
249 _kdc_make_anonymous_principalname (PrincipalName
*pn
)
251 pn
->name_type
= KRB5_NT_PRINCIPAL
;
252 pn
->name_string
.len
= 1;
253 pn
->name_string
.val
= malloc(sizeof(*pn
->name_string
.val
));
254 if (pn
->name_string
.val
== NULL
)
256 pn
->name_string
.val
[0] = strdup("anonymous");
257 if (pn
->name_string
.val
[0] == NULL
) {
258 free(pn
->name_string
.val
);
259 pn
->name_string
.val
= NULL
;
266 _kdc_r_log(kdc_request_t r
, int level
, const char *fmt
, ...)
271 s
= kdc_log_msg_va(r
->context
, r
->config
, level
, fmt
, ap
);
277 _kdc_set_e_text(kdc_request_t r
, const char *e_text
)
280 kdc_log(r
->context
, r
->config
, 0, "%s", e_text
);
284 _kdc_log_timestamp(krb5_context context
,
285 krb5_kdc_configuration
*config
,
287 KerberosTime authtime
, KerberosTime
*starttime
,
288 KerberosTime endtime
, KerberosTime
*renew_till
)
290 char authtime_str
[100], starttime_str
[100],
291 endtime_str
[100], renewtime_str
[100];
293 krb5_format_time(context
, authtime
,
294 authtime_str
, sizeof(authtime_str
), TRUE
);
296 krb5_format_time(context
, *starttime
,
297 starttime_str
, sizeof(starttime_str
), TRUE
);
299 strlcpy(starttime_str
, "unset", sizeof(starttime_str
));
300 krb5_format_time(context
, endtime
,
301 endtime_str
, sizeof(endtime_str
), TRUE
);
303 krb5_format_time(context
, *renew_till
,
304 renewtime_str
, sizeof(renewtime_str
), TRUE
);
306 strlcpy(renewtime_str
, "unset", sizeof(renewtime_str
));
308 kdc_log(context
, config
, 5,
309 "%s authtime: %s starttime: %s endtime: %s renew till: %s",
310 type
, authtime_str
, starttime_str
, endtime_str
, renewtime_str
);
319 static krb5_error_code
320 pa_pkinit_validate(kdc_request_t r
, const PA_DATA
*pa
)
322 pk_client_params
*pkp
= NULL
;
323 char *client_cert
= NULL
;
326 ret
= _kdc_pk_rd_padata(r
->context
, r
->config
, &r
->req
, pa
, r
->client
, &pkp
);
327 if (ret
|| pkp
== NULL
) {
328 ret
= KRB5KRB_AP_ERR_BAD_INTEGRITY
;
329 _kdc_r_log(r
, 5, "Failed to decode PKINIT PA-DATA -- %s",
334 ret
= _kdc_pk_check_client(r
->context
,
341 _kdc_set_e_text(r
, "PKINIT certificate not allowed to "
342 "impersonate principal");
346 _kdc_r_log(r
, 0, "PKINIT pre-authentication succeeded -- %s using %s",
347 r
->client_name
, client_cert
);
350 ret
= _kdc_pk_mk_pa_reply(r
->context
, r
->config
, pkp
, r
->client
,
351 r
->sessionetype
, &r
->req
, &r
->request
,
352 &r
->reply_key
, &r
->session_key
, &r
->outpadata
);
354 _kdc_set_e_text(r
, "Failed to build PK-INIT reply");
358 ret
= _kdc_add_inital_verified_cas(r
->context
, r
->config
,
363 _kdc_pk_free_client_param(r
->context
, pkp
);
374 static krb5_error_code
375 make_pa_enc_challange(krb5_context context
, METHOD_DATA
*md
,
382 EncryptedData encdata
;
387 krb5_us_timeofday (context
, &p
.patimestamp
, &usec
);
391 ASN1_MALLOC_ENCODE(PA_ENC_TS_ENC
, buf
, buf_size
, &p
, &len
, ret
);
395 krb5_abortx(context
, "internal error in ASN.1 encoder");
397 ret
= krb5_encrypt_EncryptedData(context
,
399 KRB5_KU_ENC_CHALLENGE_KDC
,
408 ASN1_MALLOC_ENCODE(EncryptedData
, buf
, buf_size
, &encdata
, &len
, ret
);
409 free_EncryptedData(&encdata
);
413 krb5_abortx(context
, "internal error in ASN.1 encoder");
415 ret
= krb5_padata_add(context
, md
, KRB5_PADATA_ENCRYPTED_CHALLENGE
, buf
, len
);
421 static krb5_error_code
422 pa_enc_chal_validate(kdc_request_t r
, const PA_DATA
*pa
)
424 krb5_data pepper1
, pepper2
, ts_data
;
425 KDC_REQ_BODY
*b
= &r
->req
.req_body
;
426 EncryptedData enc_data
;
427 krb5_enctype aenctype
;
433 heim_assert(r
->armor_crypto
!= NULL
, "ENC-CHAL called for non FAST");
435 if (b
->kdc_options
.request_anonymous
) {
436 ret
= KRB5KRB_AP_ERR_BAD_INTEGRITY
;
437 kdc_log(r
->context
, r
->config
, 0, "ENC-CHALL doesn't support anon");
441 ret
= decode_EncryptedData(pa
->padata_value
.data
,
442 pa
->padata_value
.length
,
446 ret
= KRB5KRB_AP_ERR_BAD_INTEGRITY
;
447 _kdc_r_log(r
, 5, "Failed to decode PA-DATA -- %s",
452 pepper1
.data
= "clientchallengearmor";
453 pepper1
.length
= strlen(pepper1
.data
);
454 pepper2
.data
= "challengelongterm";
455 pepper2
.length
= strlen(pepper2
.data
);
457 krb5_crypto_getenctype(r
->context
, r
->armor_crypto
, &aenctype
);
459 for (i
= 0; i
< r
->client
->entry
.keys
.len
; i
++) {
460 krb5_crypto challangecrypto
, longtermcrypto
;
461 krb5_keyblock challangekey
;
464 k
= &r
->client
->entry
.keys
.val
[i
];
466 ret
= krb5_crypto_init(r
->context
, &k
->key
, 0, &longtermcrypto
);
470 ret
= krb5_crypto_fx_cf2(r
->context
, r
->armor_crypto
, longtermcrypto
,
471 &pepper1
, &pepper2
, aenctype
,
473 krb5_crypto_destroy(r
->context
, longtermcrypto
);
477 ret
= krb5_crypto_init(r
->context
, &challangekey
, 0,
482 ret
= krb5_decrypt_EncryptedData(r
->context
, challangecrypto
,
483 KRB5_KU_ENC_CHALLENGE_CLIENT
,
489 ret
= decode_PA_ENC_TS_ENC(ts_data
.data
,
493 krb5_data_free(&ts_data
);
495 krb5_crypto_destroy(r
->context
, challangecrypto
);
496 ret
= KRB5KDC_ERR_PREAUTH_FAILED
;
497 _kdc_r_log(r
, 5, "Failed to decode PA-ENC-TS_ENC -- %s",
502 if (abs(kdc_time
- p
.patimestamp
) > r
->context
->max_skew
) {
503 char client_time
[100];
505 krb5_crypto_destroy(r
->context
, challangecrypto
);
507 krb5_format_time(r
->context
, p
.patimestamp
,
508 client_time
, sizeof(client_time
), TRUE
);
510 ret
= KRB5KRB_AP_ERR_SKEW
;
511 _kdc_r_log(r
, 0, "Too large time skew, "
512 "client time %s is out by %u > %u seconds -- %s",
514 (unsigned)abs(kdc_time
- p
.patimestamp
),
515 r
->context
->max_skew
,
518 free_PA_ENC_TS_ENC(&p
);
522 free_PA_ENC_TS_ENC(&p
);
524 ret
= make_pa_enc_challange(r
->context
, &r
->outpadata
,
526 krb5_crypto_destroy(r
->context
, challangecrypto
);
530 set_salt_padata(&r
->outpadata
, k
->salt
);
531 krb5_free_keyblock_contents(r
->context
, &r
->reply_key
);
532 ret
= krb5_copy_keyblock_contents(r
->context
, &k
->key
, &r
->reply_key
);
538 if (i
< r
->client
->entry
.keys
.len
)
539 ret
= KRB5KDC_ERR_PREAUTH_FAILED
;
541 free_EncryptedData(&enc_data
);
546 static krb5_error_code
547 pa_enc_ts_validate(kdc_request_t r
, const PA_DATA
*pa
)
549 EncryptedData enc_data
;
558 if (r
->req
.req_body
.kdc_options
.request_anonymous
) {
559 ret
= KRB5KRB_AP_ERR_BAD_INTEGRITY
;
560 _kdc_set_e_text(r
, "ENC-TS doesn't support anon");
564 ret
= decode_EncryptedData(pa
->padata_value
.data
,
565 pa
->padata_value
.length
,
569 ret
= KRB5KRB_AP_ERR_BAD_INTEGRITY
;
570 _kdc_r_log(r
, 5, "Failed to decode PA-DATA -- %s",
575 ret
= hdb_enctype2key(r
->context
, &r
->client
->entry
, NULL
,
576 enc_data
.etype
, &pa_key
);
579 _kdc_set_e_text(r
, "No key matching entype");
580 ret
= KRB5KDC_ERR_ETYPE_NOSUPP
;
581 if(krb5_enctype_to_string(r
->context
, enc_data
.etype
, &estr
))
585 "No client key matching pa-data (%d) -- %s",
586 enc_data
.etype
, r
->client_name
);
589 "No client key matching pa-data (%s) -- %s",
590 estr
, r
->client_name
);
592 free_EncryptedData(&enc_data
);
597 ret
= krb5_crypto_init(r
->context
, &pa_key
->key
, 0, &crypto
);
599 const char *msg
= krb5_get_error_message(r
->context
, ret
);
600 _kdc_r_log(r
, 0, "krb5_crypto_init failed: %s", msg
);
601 krb5_free_error_message(r
->context
, msg
);
602 free_EncryptedData(&enc_data
);
606 ret
= krb5_decrypt_EncryptedData (r
->context
,
608 KRB5_KU_PA_ENC_TIMESTAMP
,
611 krb5_crypto_destroy(r
->context
, crypto
);
613 * Since the user might have several keys with the same
614 * enctype but with diffrent salting, we need to try all
615 * the keys with the same enctype.
618 krb5_error_code ret2
;
619 const char *msg
= krb5_get_error_message(r
->context
, ret
);
621 ret2
= krb5_enctype_to_string(r
->context
,
622 pa_key
->key
.keytype
, &str
);
625 _kdc_r_log(r
, 5, "Failed to decrypt PA-DATA -- %s "
626 "(enctype %s) error %s",
627 r
->client_name
, str
? str
: "unknown enctype", msg
);
628 krb5_free_error_message(r
->context
, msg
);
631 if(hdb_next_enctype2key(r
->context
, &r
->client
->entry
, NULL
,
632 enc_data
.etype
, &pa_key
) == 0)
635 free_EncryptedData(&enc_data
);
637 if (r
->clientdb
->hdb_auth_status
)
638 r
->clientdb
->hdb_auth_status(r
->context
, r
->clientdb
, r
->client
,
639 HDB_AUTH_WRONG_PASSWORD
);
641 ret
= KRB5KDC_ERR_PREAUTH_FAILED
;
644 free_EncryptedData(&enc_data
);
645 ret
= decode_PA_ENC_TS_ENC(ts_data
.data
,
649 krb5_data_free(&ts_data
);
651 ret
= KRB5KDC_ERR_PREAUTH_FAILED
;
652 _kdc_r_log(r
, 5, "Failed to decode PA-ENC-TS_ENC -- %s",
656 if (abs(kdc_time
- p
.patimestamp
) > r
->context
->max_skew
) {
657 char client_time
[100];
659 krb5_format_time(r
->context
, p
.patimestamp
,
660 client_time
, sizeof(client_time
), TRUE
);
662 ret
= KRB5KRB_AP_ERR_SKEW
;
663 _kdc_r_log(r
, 0, "Too large time skew, "
664 "client time %s is out by %u > %u seconds -- %s",
666 (unsigned)abs(kdc_time
- p
.patimestamp
),
667 r
->context
->max_skew
,
671 * The following is needed to make windows clients to
672 * retry using the timestamp in the error message, if
673 * there is a e_text, they become unhappy.
676 free_PA_ENC_TS_ENC(&p
);
679 free_PA_ENC_TS_ENC(&p
);
681 set_salt_padata(&r
->outpadata
, pa_key
->salt
);
683 ret
= krb5_copy_keyblock_contents(r
->context
, &pa_key
->key
, &r
->reply_key
);
687 ret
= krb5_enctype_to_string(r
->context
, pa_key
->key
.keytype
, &str
);
690 _kdc_r_log(r
, 2, "ENC-TS Pre-authentication succeeded -- %s using %s",
691 r
->client_name
, str
? str
: "unknown enctype");
705 #define PA_ANNOUNCE 1
706 #define PA_REQ_FAST 2 /* only use inside fast */
707 krb5_error_code (*validate
)(kdc_request_t
, const PA_DATA
*pa
);
710 static const struct kdc_patypes pat
[] = {
713 KRB5_PADATA_PK_AS_REQ
, "PK-INIT(ietf)", PA_ANNOUNCE
,
717 KRB5_PADATA_PK_AS_REQ_WIN
, "PK-INIT(win2k)", PA_ANNOUNCE
,
721 { KRB5_PADATA_PK_AS_REQ
, "PK-INIT(ietf)", 0, NULL
},
722 { KRB5_PADATA_PK_AS_REQ_WIN
, "PK-INIT(win2k)", 0, NULL
},
724 { KRB5_PADATA_PA_PK_OCSP_RESPONSE
, "OCSP", 0, NULL
},
726 KRB5_PADATA_ENC_TIMESTAMP
, "ENC-TS",
731 KRB5_PADATA_ENCRYPTED_CHALLENGE
, "ENC-CHAL",
732 PA_ANNOUNCE
| PA_REQ_FAST
,
735 { KRB5_PADATA_REQ_ENC_PA_REP
, "REQ-ENC-PA-REP", 0, NULL
},
736 { KRB5_PADATA_FX_FAST
, "FX-FAST", PA_ANNOUNCE
, NULL
},
737 { KRB5_PADATA_FX_ERROR
, "FX-ERROR", 0, NULL
},
738 { KRB5_PADATA_FX_COOKIE
, "FX-COOKIE", 0, NULL
}
742 log_patypes(krb5_context context
,
743 krb5_kdc_configuration
*config
,
746 struct rk_strpool
*p
= NULL
;
750 for (n
= 0; n
< padata
->len
; n
++) {
751 for (m
= 0; m
< sizeof(pat
) / sizeof(pat
[0]); m
++) {
752 if (padata
->val
[n
].padata_type
== pat
[m
].type
) {
753 p
= rk_strpoolprintf(p
, "%s", pat
[m
].name
);
757 if (m
== sizeof(pat
) / sizeof(pat
[0]))
758 p
= rk_strpoolprintf(p
, "%d", padata
->val
[n
].padata_type
);
759 if (p
&& n
+ 1 < padata
->len
)
760 p
= rk_strpoolprintf(p
, ", ");
762 kdc_log(context
, config
, 0, "out of memory");
767 p
= rk_strpoolprintf(p
, "none");
769 str
= rk_strpoolcollect(p
);
770 kdc_log(context
, config
, 0, "Client sent patypes: %s", str
);
779 _kdc_encode_reply(krb5_context context
,
780 krb5_kdc_configuration
*config
,
781 krb5_crypto armor_crypto
, uint32_t nonce
,
782 KDC_REP
*rep
, EncTicketPart
*et
, EncKDCRepPart
*ek
,
784 int skvno
, const EncryptionKey
*skey
,
785 int ckvno
, const EncryptionKey
*reply_key
,
796 ASN1_MALLOC_ENCODE(EncTicketPart
, buf
, buf_size
, et
, &len
, ret
);
798 const char *msg
= krb5_get_error_message(context
, ret
);
799 kdc_log(context
, config
, 0, "Failed to encode ticket: %s", msg
);
800 krb5_free_error_message(context
, msg
);
804 krb5_abortx(context
, "Internal error in ASN.1 encoder");
806 ret
= krb5_crypto_init(context
, skey
, etype
, &crypto
);
808 const char *msg
= krb5_get_error_message(context
, ret
);
809 kdc_log(context
, config
, 0, "krb5_crypto_init failed: %s", msg
);
810 krb5_free_error_message(context
, msg
);
814 ret
= krb5_encrypt_EncryptedData(context
,
820 &rep
->ticket
.enc_part
);
822 krb5_crypto_destroy(context
, crypto
);
824 const char *msg
= krb5_get_error_message(context
, ret
);
825 kdc_log(context
, config
, 0, "Failed to encrypt data: %s", msg
);
826 krb5_free_error_message(context
, msg
);
832 krb5_keyblock
*strengthen_key
= NULL
;
833 KrbFastFinished finished
;
835 kdc_log(context
, config
, 0, "FAST armor protection");
837 memset(&finished
, 0, sizeof(finished
));
838 krb5_data_zero(&data
);
840 finished
.timestamp
= kdc_time
;
842 finished
.crealm
= et
->crealm
;
843 finished
.cname
= et
->cname
;
845 ASN1_MALLOC_ENCODE(Ticket
, data
.data
, data
.length
,
846 &rep
->ticket
, &len
, ret
);
849 if (data
.length
!= len
)
850 krb5_abortx(context
, "internal asn.1 error");
852 ret
= krb5_create_checksum(context
, armor_crypto
,
853 KRB5_KU_FAST_FINISHED
, 0,
854 data
.data
, data
.length
,
855 &finished
.ticket_checksum
);
856 krb5_data_free(&data
);
860 ret
= _kdc_fast_mk_response(context
, armor_crypto
,
861 rep
->padata
, strengthen_key
, &finished
,
863 free_Checksum(&finished
.ticket_checksum
);
868 free_METHOD_DATA(rep
->padata
);
870 rep
->padata
= calloc(1, sizeof(*(rep
->padata
)));
871 if (rep
->padata
== NULL
) {
872 krb5_data_free(&data
);
877 ret
= krb5_padata_add(context
, rep
->padata
,
879 data
.data
, data
.length
);
884 * Hide client name of privacy reasons
886 if (1 /* r->fast_options.hide_client_names */) {
887 rep
->crealm
[0] = '\0';
888 free_PrincipalName(&rep
->cname
);
889 rep
->cname
.name_type
= 0;
893 if(rep
->msg_type
== krb_as_rep
&& !config
->encode_as_rep_as_tgs_rep
)
894 ASN1_MALLOC_ENCODE(EncASRepPart
, buf
, buf_size
, ek
, &len
, ret
);
896 ASN1_MALLOC_ENCODE(EncTGSRepPart
, buf
, buf_size
, ek
, &len
, ret
);
898 const char *msg
= krb5_get_error_message(context
, ret
);
899 kdc_log(context
, config
, 0, "Failed to encode KDC-REP: %s", msg
);
900 krb5_free_error_message(context
, msg
);
903 if(buf_size
!= len
) {
905 kdc_log(context
, config
, 0, "Internal error in ASN.1 encoder");
906 *e_text
= "KDC internal error";
907 return KRB5KRB_ERR_GENERIC
;
909 ret
= krb5_crypto_init(context
, reply_key
, 0, &crypto
);
911 const char *msg
= krb5_get_error_message(context
, ret
);
913 kdc_log(context
, config
, 0, "krb5_crypto_init failed: %s", msg
);
914 krb5_free_error_message(context
, msg
);
917 if(rep
->msg_type
== krb_as_rep
) {
918 krb5_encrypt_EncryptedData(context
,
920 KRB5_KU_AS_REP_ENC_PART
,
926 ASN1_MALLOC_ENCODE(AS_REP
, buf
, buf_size
, rep
, &len
, ret
);
928 krb5_encrypt_EncryptedData(context
,
930 rk_is_subkey
? KRB5_KU_TGS_REP_ENC_PART_SUB_KEY
: KRB5_KU_TGS_REP_ENC_PART_SESSION
,
936 ASN1_MALLOC_ENCODE(TGS_REP
, buf
, buf_size
, rep
, &len
, ret
);
938 krb5_crypto_destroy(context
, crypto
);
940 const char *msg
= krb5_get_error_message(context
, ret
);
941 kdc_log(context
, config
, 0, "Failed to encode KDC-REP: %s", msg
);
942 krb5_free_error_message(context
, msg
);
945 if(buf_size
!= len
) {
947 kdc_log(context
, config
, 0, "Internal error in ASN.1 encoder");
948 *e_text
= "KDC internal error";
949 return KRB5KRB_ERR_GENERIC
;
952 reply
->length
= buf_size
;
957 * Return 1 if the client have only older enctypes, this is for
958 * determining if the server should send ETYPE_INFO2 or not.
962 older_enctype(krb5_enctype enctype
)
965 case ETYPE_DES_CBC_CRC
:
966 case ETYPE_DES_CBC_MD4
:
967 case ETYPE_DES_CBC_MD5
:
968 case ETYPE_DES3_CBC_SHA1
:
969 case ETYPE_ARCFOUR_HMAC_MD5
:
970 case ETYPE_ARCFOUR_HMAC_MD5_56
:
972 * The following three is "old" windows enctypes and is needed for
973 * windows 2000 hosts.
975 case ETYPE_ARCFOUR_MD4
:
976 case ETYPE_ARCFOUR_HMAC_OLD
:
977 case ETYPE_ARCFOUR_HMAC_OLD_EXP
:
988 static krb5_error_code
989 make_etype_info_entry(krb5_context context
, ETYPE_INFO_ENTRY
*ent
, Key
*key
)
991 ent
->etype
= key
->key
.keytype
;
994 ALLOC(ent
->salttype
);
996 if(key
->salt
->type
== hdb_pw_salt
)
997 *ent
->salttype
= 0; /* or 1? or NULL? */
998 else if(key
->salt
->type
== hdb_afs3_salt
)
1001 kdc_log(context
, config
, 0, "unknown salt-type: %d",
1003 return KRB5KRB_ERR_GENERIC
;
1005 /* according to `the specs', we can't send a salt if
1006 we have AFS3 salted key, but that requires that you
1007 *know* what cell you are using (e.g by assuming
1008 that the cell is the same as the realm in lower
1011 ALLOC(ent
->salttype
);
1012 *ent
->salttype
= key
->salt
->type
;
1015 * We shouldn't sent salttype since it is incompatible with the
1016 * specification and it breaks windows clients. The afs
1017 * salting problem is solved by using KRB5-PADATA-AFS3-SALT
1018 * implemented in Heimdal 0.7 and later.
1020 ent
->salttype
= NULL
;
1022 krb5_copy_data(context
, &key
->salt
->salt
,
1025 /* we return no salt type at all, as that should indicate
1026 * the default salt type and make everybody happy. some
1027 * systems (like w2k) dislike being told the salt type
1030 ent
->salttype
= NULL
;
1036 static krb5_error_code
1037 get_pa_etype_info(krb5_context context
,
1038 krb5_kdc_configuration
*config
,
1039 METHOD_DATA
*md
, Key
*ckey
)
1041 krb5_error_code ret
= 0;
1048 pa
.val
= calloc(1, sizeof(pa
.val
[0]));
1052 ret
= make_etype_info_entry(context
, &pa
.val
[0], ckey
);
1054 free_ETYPE_INFO(&pa
);
1058 ASN1_MALLOC_ENCODE(ETYPE_INFO
, buf
, len
, &pa
, &len
, ret
);
1059 free_ETYPE_INFO(&pa
);
1062 ret
= realloc_method_data(md
);
1067 md
->val
[md
->len
- 1].padata_type
= KRB5_PADATA_ETYPE_INFO
;
1068 md
->val
[md
->len
- 1].padata_value
.length
= len
;
1069 md
->val
[md
->len
- 1].padata_value
.data
= buf
;
1077 extern int _krb5_AES_string_to_default_iterator
;
1079 static krb5_error_code
1080 make_etype_info2_entry(ETYPE_INFO2_ENTRY
*ent
, Key
*key
)
1082 ent
->etype
= key
->key
.keytype
;
1085 if (ent
->salt
== NULL
)
1087 *ent
->salt
= malloc(key
->salt
->salt
.length
+ 1);
1088 if (*ent
->salt
== NULL
) {
1093 memcpy(*ent
->salt
, key
->salt
->salt
.data
, key
->salt
->salt
.length
);
1094 (*ent
->salt
)[key
->salt
->salt
.length
] = '\0';
1098 ent
->s2kparams
= NULL
;
1100 switch (key
->key
.keytype
) {
1101 case ETYPE_AES128_CTS_HMAC_SHA1_96
:
1102 case ETYPE_AES256_CTS_HMAC_SHA1_96
:
1103 ALLOC(ent
->s2kparams
);
1104 if (ent
->s2kparams
== NULL
)
1106 ent
->s2kparams
->length
= 4;
1107 ent
->s2kparams
->data
= malloc(ent
->s2kparams
->length
);
1108 if (ent
->s2kparams
->data
== NULL
) {
1109 free(ent
->s2kparams
);
1110 ent
->s2kparams
= NULL
;
1113 _krb5_put_int(ent
->s2kparams
->data
,
1114 _krb5_AES_string_to_default_iterator
,
1115 ent
->s2kparams
->length
);
1117 case ETYPE_DES_CBC_CRC
:
1118 case ETYPE_DES_CBC_MD4
:
1119 case ETYPE_DES_CBC_MD5
:
1120 /* Check if this was a AFS3 salted key */
1121 if(key
->salt
&& key
->salt
->type
== hdb_afs3_salt
){
1122 ALLOC(ent
->s2kparams
);
1123 if (ent
->s2kparams
== NULL
)
1125 ent
->s2kparams
->length
= 1;
1126 ent
->s2kparams
->data
= malloc(ent
->s2kparams
->length
);
1127 if (ent
->s2kparams
->data
== NULL
) {
1128 free(ent
->s2kparams
);
1129 ent
->s2kparams
= NULL
;
1132 _krb5_put_int(ent
->s2kparams
->data
,
1134 ent
->s2kparams
->length
);
1144 * Return an ETYPE-INFO2. Enctypes are storted the same way as in the
1145 * database (client supported enctypes first, then the unsupported
1149 static krb5_error_code
1150 get_pa_etype_info2(krb5_context context
,
1151 krb5_kdc_configuration
*config
,
1152 METHOD_DATA
*md
, Key
*ckey
)
1154 krb5_error_code ret
= 0;
1160 pa
.val
= calloc(1, sizeof(pa
.val
[0]));
1164 ret
= make_etype_info2_entry(&pa
.val
[0], ckey
);
1166 free_ETYPE_INFO2(&pa
);
1170 ASN1_MALLOC_ENCODE(ETYPE_INFO2
, buf
, len
, &pa
, &len
, ret
);
1171 free_ETYPE_INFO2(&pa
);
1174 ret
= realloc_method_data(md
);
1179 md
->val
[md
->len
- 1].padata_type
= KRB5_PADATA_ETYPE_INFO2
;
1180 md
->val
[md
->len
- 1].padata_value
.length
= len
;
1181 md
->val
[md
->len
- 1].padata_value
.data
= buf
;
1190 log_as_req(krb5_context context
,
1191 krb5_kdc_configuration
*config
,
1192 krb5_enctype cetype
,
1193 krb5_enctype setype
,
1194 const KDC_REQ_BODY
*b
)
1196 krb5_error_code ret
;
1197 struct rk_strpool
*p
;
1201 p
= rk_strpoolprintf(NULL
, "%s", "Client supported enctypes: ");
1203 for (i
= 0; i
< b
->etype
.len
; i
++) {
1204 ret
= krb5_enctype_to_string(context
, b
->etype
.val
[i
], &str
);
1206 p
= rk_strpoolprintf(p
, "%s", str
);
1209 p
= rk_strpoolprintf(p
, "%d", b
->etype
.val
[i
]);
1210 if (p
&& i
+ 1 < b
->etype
.len
)
1211 p
= rk_strpoolprintf(p
, ", ");
1213 kdc_log(context
, config
, 0, "out of memory");
1218 p
= rk_strpoolprintf(p
, "no encryption types");
1224 ret
= krb5_enctype_to_string(context
, cetype
, &cet
);
1226 ret
= krb5_enctype_to_string(context
, setype
, &set
);
1228 p
= rk_strpoolprintf(p
, ", using %s/%s", cet
, set
);
1234 p
= rk_strpoolprintf(p
, ", using enctypes %d/%d",
1238 str
= rk_strpoolcollect(p
);
1239 kdc_log(context
, config
, 0, "%s", str
);
1244 unparse_flags(KDCOptions2int(b
->kdc_options
), asn1_KDCOptions_units(),
1245 fixedstr
, sizeof(fixedstr
));
1247 kdc_log(context
, config
, 0, "Requested flags: %s", fixedstr
);
1252 * verify the flags on `client' and `server', returning 0
1253 * if they are OK and generating an error messages and returning
1254 * and error code otherwise.
1258 kdc_check_flags(krb5_context context
,
1259 krb5_kdc_configuration
*config
,
1260 hdb_entry_ex
*client_ex
, const char *client_name
,
1261 hdb_entry_ex
*server_ex
, const char *server_name
,
1262 krb5_boolean is_as_req
)
1264 if(client_ex
!= NULL
) {
1265 hdb_entry
*client
= &client_ex
->entry
;
1268 if (client
->flags
.locked_out
) {
1269 kdc_log(context
, config
, 0,
1270 "Client (%s) is locked out", client_name
);
1271 return KRB5KDC_ERR_POLICY
;
1274 if (client
->flags
.invalid
) {
1275 kdc_log(context
, config
, 0,
1276 "Client (%s) has invalid bit set", client_name
);
1277 return KRB5KDC_ERR_POLICY
;
1280 if(!client
->flags
.client
){
1281 kdc_log(context
, config
, 0,
1282 "Principal may not act as client -- %s", client_name
);
1283 return KRB5KDC_ERR_POLICY
;
1286 if (client
->valid_start
&& *client
->valid_start
> kdc_time
) {
1287 char starttime_str
[100];
1288 krb5_format_time(context
, *client
->valid_start
,
1289 starttime_str
, sizeof(starttime_str
), TRUE
);
1290 kdc_log(context
, config
, 0,
1291 "Client not yet valid until %s -- %s",
1292 starttime_str
, client_name
);
1293 return KRB5KDC_ERR_CLIENT_NOTYET
;
1296 if (client
->valid_end
&& *client
->valid_end
< kdc_time
) {
1297 char endtime_str
[100];
1298 krb5_format_time(context
, *client
->valid_end
,
1299 endtime_str
, sizeof(endtime_str
), TRUE
);
1300 kdc_log(context
, config
, 0,
1301 "Client expired at %s -- %s",
1302 endtime_str
, client_name
);
1303 return KRB5KDC_ERR_NAME_EXP
;
1306 if (client
->flags
.require_pwchange
&&
1307 (server_ex
== NULL
|| !server_ex
->entry
.flags
.change_pw
)) {
1308 kdc_log(context
, config
, 0,
1309 "Client's key must be changed -- %s", client_name
);
1310 return KRB5KDC_ERR_KEY_EXPIRED
;
1313 if (client
->pw_end
&& *client
->pw_end
< kdc_time
1314 && (server_ex
== NULL
|| !server_ex
->entry
.flags
.change_pw
)) {
1315 char pwend_str
[100];
1316 krb5_format_time(context
, *client
->pw_end
,
1317 pwend_str
, sizeof(pwend_str
), TRUE
);
1318 kdc_log(context
, config
, 0,
1319 "Client's key has expired at %s -- %s",
1320 pwend_str
, client_name
);
1321 return KRB5KDC_ERR_KEY_EXPIRED
;
1327 if (server_ex
!= NULL
) {
1328 hdb_entry
*server
= &server_ex
->entry
;
1330 if (server
->flags
.locked_out
) {
1331 kdc_log(context
, config
, 0,
1332 "Client server locked out -- %s", server_name
);
1333 return KRB5KDC_ERR_POLICY
;
1335 if (server
->flags
.invalid
) {
1336 kdc_log(context
, config
, 0,
1337 "Server has invalid flag set -- %s", server_name
);
1338 return KRB5KDC_ERR_POLICY
;
1341 if(!server
->flags
.server
){
1342 kdc_log(context
, config
, 0,
1343 "Principal may not act as server -- %s", server_name
);
1344 return KRB5KDC_ERR_POLICY
;
1347 if(!is_as_req
&& server
->flags
.initial
) {
1348 kdc_log(context
, config
, 0,
1349 "AS-REQ is required for server -- %s", server_name
);
1350 return KRB5KDC_ERR_POLICY
;
1353 if (server
->valid_start
&& *server
->valid_start
> kdc_time
) {
1354 char starttime_str
[100];
1355 krb5_format_time(context
, *server
->valid_start
,
1356 starttime_str
, sizeof(starttime_str
), TRUE
);
1357 kdc_log(context
, config
, 0,
1358 "Server not yet valid until %s -- %s",
1359 starttime_str
, server_name
);
1360 return KRB5KDC_ERR_SERVICE_NOTYET
;
1363 if (server
->valid_end
&& *server
->valid_end
< kdc_time
) {
1364 char endtime_str
[100];
1365 krb5_format_time(context
, *server
->valid_end
,
1366 endtime_str
, sizeof(endtime_str
), TRUE
);
1367 kdc_log(context
, config
, 0,
1368 "Server expired at %s -- %s",
1369 endtime_str
, server_name
);
1370 return KRB5KDC_ERR_SERVICE_EXP
;
1373 if (server
->pw_end
&& *server
->pw_end
< kdc_time
) {
1374 char pwend_str
[100];
1375 krb5_format_time(context
, *server
->pw_end
,
1376 pwend_str
, sizeof(pwend_str
), TRUE
);
1377 kdc_log(context
, config
, 0,
1378 "Server's key has expired at -- %s",
1379 pwend_str
, server_name
);
1380 return KRB5KDC_ERR_KEY_EXPIRED
;
1387 * Return TRUE if `from' is part of `addresses' taking into consideration
1388 * the configuration variables that tells us how strict we should be about
1393 _kdc_check_addresses(krb5_context context
,
1394 krb5_kdc_configuration
*config
,
1395 HostAddresses
*addresses
, const struct sockaddr
*from
)
1397 krb5_error_code ret
;
1399 krb5_boolean result
;
1400 krb5_boolean only_netbios
= TRUE
;
1403 if(config
->check_ticket_addresses
== 0)
1406 if(addresses
== NULL
)
1407 return config
->allow_null_ticket_addresses
;
1409 for (i
= 0; i
< addresses
->len
; ++i
) {
1410 if (addresses
->val
[i
].addr_type
!= KRB5_ADDRESS_NETBIOS
) {
1411 only_netbios
= FALSE
;
1415 /* Windows sends it's netbios name, which I can only assume is
1416 * used for the 'allowed workstations' check. This is painful,
1417 * but we still want to check IP addresses if they happen to be
1422 return config
->allow_null_ticket_addresses
;
1424 ret
= krb5_sockaddr2address (context
, from
, &addr
);
1428 result
= krb5_address_search(context
, &addr
, addresses
);
1429 krb5_free_address (context
, &addr
);
1438 send_pac_p(krb5_context context
, KDC_REQ
*req
)
1440 krb5_error_code ret
;
1441 PA_PAC_REQUEST pacreq
;
1445 pa
= _kdc_find_padata(req
, &i
, KRB5_PADATA_PA_PAC_REQUEST
);
1449 ret
= decode_PA_PAC_REQUEST(pa
->padata_value
.data
,
1450 pa
->padata_value
.length
,
1455 i
= pacreq
.include_pac
;
1456 free_PA_PAC_REQUEST(&pacreq
);
1466 static krb5_error_code
1467 generate_pac(kdc_request_t r
, Key
*skey
)
1469 krb5_error_code ret
;
1473 ret
= _kdc_pac_generate(r
->context
, r
->client
, &p
);
1475 _kdc_r_log(r
, 0, "PAC generation failed for -- %s",
1482 ret
= _krb5_pac_sign(r
->context
, p
, r
->et
.authtime
,
1483 r
->client
->entry
.principal
,
1484 &skey
->key
, /* Server key */
1485 &skey
->key
, /* FIXME: should be krbtgt key */
1487 krb5_pac_free(r
->context
, p
);
1489 _kdc_r_log(r
, 0, "PAC signing failed for -- %s",
1494 ret
= _kdc_tkt_add_if_relevant_ad(r
->context
, &r
->et
,
1495 KRB5_AUTHDATA_WIN2K_PAC
,
1497 krb5_data_free(&data
);
1507 _kdc_is_anonymous(krb5_context context
, krb5_principal principal
)
1509 if (principal
->name
.name_type
!= KRB5_NT_WELLKNOWN
||
1510 principal
->name
.name_string
.len
!= 2 ||
1511 strcmp(principal
->name
.name_string
.val
[0], KRB5_WELLKNOWN_NAME
) != 0 ||
1512 strcmp(principal
->name
.name_string
.val
[1], KRB5_ANON_NAME
) != 0)
1518 require_preauth_p(kdc_request_t r
)
1520 return r
->config
->require_preauth
1521 || r
->client
->entry
.flags
.require_preauth
1522 || r
->server
->entry
.flags
.require_preauth
;
1530 static krb5_error_code
1531 add_enc_pa_rep(kdc_request_t r
)
1533 krb5_error_code ret
;
1539 ret
= krb5_crypto_init(r
->context
, &r
->reply_key
, 0, &crypto
);
1543 ret
= krb5_create_checksum(r
->context
, crypto
,
1545 r
->request
.data
, r
->request
.length
,
1547 krb5_crypto_destroy(r
->context
, crypto
);
1551 ASN1_MALLOC_ENCODE(Checksum
, cdata
.data
, cdata
.length
,
1552 &checksum
, &len
, ret
);
1553 free_Checksum(&checksum
);
1556 heim_assert(cdata
.length
== len
, "ASN.1 internal error");
1558 if (r
->ek
.encrypted_pa_data
== NULL
) {
1559 ALLOC(r
->ek
.encrypted_pa_data
);
1560 if (r
->ek
.encrypted_pa_data
== NULL
)
1563 ret
= krb5_padata_add(r
->context
, r
->ek
.encrypted_pa_data
,
1564 KRB5_PADATA_REQ_ENC_PA_REP
, cdata
.data
, cdata
.length
);
1568 return krb5_padata_add(r
->context
, r
->ek
.encrypted_pa_data
,
1569 KRB5_PADATA_FX_FAST
, NULL
, 0);
1577 _kdc_as_rep(kdc_request_t r
,
1580 struct sockaddr
*from_addr
,
1583 krb5_context context
= r
->context
;
1584 krb5_kdc_configuration
*config
= r
->config
;
1585 KDC_REQ
*req
= &r
->req
;
1586 KDC_REQ_BODY
*b
= NULL
;
1589 krb5_enctype setype
;
1590 krb5_error_code ret
= 0;
1593 int i
, flags
= HDB_F_FOR_AS_REQ
;
1594 METHOD_DATA error_method
;
1597 memset(&rep
, 0, sizeof(rep
));
1598 error_method
.len
= 0;
1599 error_method
.val
= NULL
;
1602 * Look for FAST armor and unwrap
1604 ret
= _kdc_fast_unwrap_request(r
);
1606 _kdc_r_log(r
, 0, "FAST unwrap request from %s failed: %d", from
, ret
);
1614 flags
|= HDB_F_CANON
;
1616 if(b
->sname
== NULL
){
1617 ret
= KRB5KRB_ERR_GENERIC
;
1618 _kdc_set_e_text(r
, "No server in request");
1620 ret
= _krb5_principalname2krb5_principal (context
,
1625 ret
= krb5_unparse_name(context
, r
->server_princ
, &r
->server_name
);
1628 kdc_log(context
, config
, 0,
1629 "AS-REQ malformed server name from %s", from
);
1632 if(b
->cname
== NULL
){
1633 ret
= KRB5KRB_ERR_GENERIC
;
1634 _kdc_set_e_text(r
, "No client in request");
1636 ret
= _krb5_principalname2krb5_principal (context
,
1643 ret
= krb5_unparse_name(context
, r
->client_princ
, &r
->client_name
);
1646 kdc_log(context
, config
, 0,
1647 "AS-REQ malformed client name from %s", from
);
1651 kdc_log(context
, config
, 0, "AS-REQ %s from %s for %s",
1652 r
->client_name
, from
, r
->server_name
);
1658 if (_kdc_is_anonymous(context
, r
->client_princ
)) {
1659 if (!b
->kdc_options
.request_anonymous
) {
1660 kdc_log(context
, config
, 0, "Anonymous ticket w/o anonymous flag");
1661 ret
= KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN
;
1664 } else if (b
->kdc_options
.request_anonymous
) {
1665 kdc_log(context
, config
, 0,
1666 "Request for a anonymous ticket with non "
1667 "anonymous client name: %s", r
->client_name
);
1668 ret
= KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN
;
1676 ret
= _kdc_db_fetch(context
, config
, r
->client_princ
,
1677 HDB_F_GET_CLIENT
| flags
, NULL
,
1678 &r
->clientdb
, &r
->client
);
1679 if(ret
== HDB_ERR_NOT_FOUND_HERE
) {
1680 kdc_log(context
, config
, 5, "client %s does not have secrets at this KDC, need to proxy",
1684 const char *msg
= krb5_get_error_message(context
, ret
);
1685 kdc_log(context
, config
, 0, "UNKNOWN -- %s: %s", r
->client_name
, msg
);
1686 krb5_free_error_message(context
, msg
);
1687 ret
= KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN
;
1690 ret
= _kdc_db_fetch(context
, config
, r
->server_princ
,
1691 HDB_F_GET_SERVER
|HDB_F_GET_KRBTGT
| flags
,
1692 NULL
, NULL
, &r
->server
);
1693 if(ret
== HDB_ERR_NOT_FOUND_HERE
) {
1694 kdc_log(context
, config
, 5, "target %s does not have secrets at this KDC, need to proxy",
1698 const char *msg
= krb5_get_error_message(context
, ret
);
1699 kdc_log(context
, config
, 0, "UNKNOWN -- %s: %s", r
->server_name
, msg
);
1700 krb5_free_error_message(context
, msg
);
1701 ret
= KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN
;
1706 * Select a session enctype from the list of the crypto system
1707 * supported enctypes that is supported by the client and is one of
1708 * the enctype of the enctype of the service (likely krbtgt).
1710 * The latter is used as a hint of what enctypes all KDC support,
1711 * to make sure a newer version of KDC won't generate a session
1712 * enctype that an older version of a KDC in the same realm can't
1716 ret
= _kdc_find_etype(context
,
1717 krb5_principal_is_krbtgt(context
, r
->server_princ
) ?
1718 config
->tgt_use_strongest_session_key
:
1719 config
->svc_use_strongest_session_key
, FALSE
,
1720 r
->client
, b
->etype
.val
, b
->etype
.len
, &r
->sessionetype
,
1723 kdc_log(context
, config
, 0,
1724 "Client (%s) from %s has no common enctypes with KDC "
1725 "to use for the session key",
1726 r
->client_name
, from
);
1731 * Pre-auth processing
1737 log_patypes(context
, config
, req
->padata
);
1739 /* Check if preauth matching */
1741 for (n
= 0; !found_pa
&& n
< sizeof(pat
) / sizeof(pat
[0]); n
++) {
1742 if (pat
[n
].validate
== NULL
)
1744 if (r
->armor_crypto
== NULL
&& (pat
[n
].flags
& PA_REQ_FAST
))
1747 kdc_log(context
, config
, 5,
1748 "Looking for %s pa-data -- %s", pat
[n
].name
, r
->client_name
);
1750 pa
= _kdc_find_padata(req
, &i
, pat
[n
].type
);
1752 ret
= pat
[n
].validate(r
, pa
);
1754 kdc_log(context
, config
, 0,
1755 "%s pre-authentication succeeded -- %s",
1756 pat
[n
].name
, r
->client_name
);
1758 r
->et
.flags
.pre_authent
= 1;
1764 if (found_pa
== 0) {
1768 for (n
= 0; n
< sizeof(pat
) / sizeof(pat
[0]); n
++) {
1769 if ((pat
[n
].flags
& PA_ANNOUNCE
) == 0)
1771 ret
= krb5_padata_add(context
, &error_method
,
1772 pat
[n
].type
, NULL
, 0);
1778 * If there is a client key, send ETYPE_INFO{,2}
1780 ret
= _kdc_find_etype(context
,
1781 config
->preauth_use_strongest_session_key
, TRUE
,
1782 r
->client
, b
->etype
.val
, b
->etype
.len
, NULL
, &ckey
);
1787 * - If the client only knows about old enctypes, then send
1788 * both info replies (we send 'info' first in the list).
1789 * - If the client is 'modern', because it knows about 'new'
1790 * enctype types, then only send the 'info2' reply.
1792 * Before we send the full list of etype-info data, we pick
1793 * the client key we would have used anyway below, just pick
1797 if (older_enctype(ckey
->key
.keytype
)) {
1798 ret
= get_pa_etype_info(context
, config
,
1799 &error_method
, ckey
);
1803 ret
= get_pa_etype_info2(context
, config
,
1804 &error_method
, ckey
);
1810 * send requre preauth is its required or anon is requested,
1811 * anon is today only allowed via preauth mechanisms.
1813 if (require_preauth_p(r
) || b
->kdc_options
.request_anonymous
) {
1814 ret
= KRB5KDC_ERR_PREAUTH_REQUIRED
;
1815 _kdc_set_e_text(r
, "Need to use PA-ENC-TIMESTAMP/PA-PK-AS-REQ");
1820 ret
= KRB5KDC_ERR_CLIENT_NOTYET
;
1821 _kdc_set_e_text(r
, "Doesn't have a client key available");
1824 krb5_free_keyblock_contents(r
->context
, &r
->reply_key
);
1825 ret
= krb5_copy_keyblock_contents(r
->context
, &ckey
->key
, &r
->reply_key
);
1830 if (r
->clientdb
->hdb_auth_status
)
1831 r
->clientdb
->hdb_auth_status(context
, r
->clientdb
, r
->client
,
1835 * Verify flags after the user been required to prove its identity
1836 * with in a preauth mech.
1839 ret
= _kdc_check_access(context
, config
, r
->client
, r
->client_name
,
1840 r
->server
, r
->server_name
,
1841 req
, &error_method
);
1846 * Select the best encryption type for the KDC with out regard to
1847 * the client since the client never needs to read that data.
1850 ret
= _kdc_get_preferred_key(context
, config
,
1851 r
->server
, r
->server_name
,
1856 if(f
.renew
|| f
.validate
|| f
.proxy
|| f
.forwarded
|| f
.enc_tkt_in_skey
1857 || (f
.request_anonymous
&& !config
->allow_anonymous
)) {
1858 ret
= KRB5KDC_ERR_BADOPTION
;
1859 _kdc_set_e_text(r
, "Bad KDC options");
1868 rep
.msg_type
= krb_as_rep
;
1870 ret
= copy_Realm(&r
->client
->entry
.principal
->realm
, &rep
.crealm
);
1873 ret
= _krb5_principal2principalname(&rep
.cname
, r
->client
->entry
.principal
);
1877 rep
.ticket
.tkt_vno
= 5;
1878 copy_Realm(&r
->server
->entry
.principal
->realm
, &rep
.ticket
.realm
);
1879 _krb5_principal2principalname(&rep
.ticket
.sname
,
1880 r
->server
->entry
.principal
);
1881 /* java 1.6 expects the name to be the same type, lets allow that
1882 * uncomplicated name-types. */
1883 #define CNT(sp,t) (((sp)->sname->name_type) == KRB5_NT_##t)
1884 if (CNT(b
, UNKNOWN
) || CNT(b
, PRINCIPAL
) || CNT(b
, SRV_INST
) || CNT(b
, SRV_HST
) || CNT(b
, SRV_XHST
))
1885 rep
.ticket
.sname
.name_type
= b
->sname
->name_type
;
1888 r
->et
.flags
.initial
= 1;
1889 if(r
->client
->entry
.flags
.forwardable
&& r
->server
->entry
.flags
.forwardable
)
1890 r
->et
.flags
.forwardable
= f
.forwardable
;
1891 else if (f
.forwardable
) {
1892 _kdc_set_e_text(r
, "Ticket may not be forwardable");
1893 ret
= KRB5KDC_ERR_POLICY
;
1896 if(r
->client
->entry
.flags
.proxiable
&& r
->server
->entry
.flags
.proxiable
)
1897 r
->et
.flags
.proxiable
= f
.proxiable
;
1898 else if (f
.proxiable
) {
1899 _kdc_set_e_text(r
, "Ticket may not be proxiable");
1900 ret
= KRB5KDC_ERR_POLICY
;
1903 if(r
->client
->entry
.flags
.postdate
&& r
->server
->entry
.flags
.postdate
)
1904 r
->et
.flags
.may_postdate
= f
.allow_postdate
;
1905 else if (f
.allow_postdate
){
1906 _kdc_set_e_text(r
, "Ticket may not be postdate");
1907 ret
= KRB5KDC_ERR_POLICY
;
1911 /* check for valid set of addresses */
1912 if(!_kdc_check_addresses(context
, config
, b
->addresses
, from_addr
)) {
1913 _kdc_set_e_text(r
, "Bad address list in requested");
1914 ret
= KRB5KRB_AP_ERR_BADADDR
;
1918 ret
= copy_PrincipalName(&rep
.cname
, &r
->et
.cname
);
1921 ret
= copy_Realm(&rep
.crealm
, &r
->et
.crealm
);
1929 start
= r
->et
.authtime
= kdc_time
;
1931 if(f
.postdated
&& req
->req_body
.from
){
1932 ALLOC(r
->et
.starttime
);
1933 start
= *r
->et
.starttime
= *req
->req_body
.from
;
1934 r
->et
.flags
.invalid
= 1;
1935 r
->et
.flags
.postdated
= 1; /* XXX ??? */
1937 _kdc_fix_time(&b
->till
);
1940 /* be careful not overflowing */
1942 if(r
->client
->entry
.max_life
)
1943 t
= start
+ min(t
- start
, *r
->client
->entry
.max_life
);
1944 if(r
->server
->entry
.max_life
)
1945 t
= start
+ min(t
- start
, *r
->server
->entry
.max_life
);
1947 t
= min(t
, start
+ realm
->max_life
);
1950 if(f
.renewable_ok
&& r
->et
.endtime
< *b
->till
){
1952 if(b
->rtime
== NULL
){
1956 if(*b
->rtime
< *b
->till
)
1957 *b
->rtime
= *b
->till
;
1959 if(f
.renewable
&& b
->rtime
){
1963 if(r
->client
->entry
.max_renew
)
1964 t
= start
+ min(t
- start
, *r
->client
->entry
.max_renew
);
1965 if(r
->server
->entry
.max_renew
)
1966 t
= start
+ min(t
- start
, *r
->server
->entry
.max_renew
);
1968 t
= min(t
, start
+ realm
->max_renew
);
1970 ALLOC(r
->et
.renew_till
);
1971 *r
->et
.renew_till
= t
;
1972 r
->et
.flags
.renewable
= 1;
1976 if (f
.request_anonymous
)
1977 r
->et
.flags
.anonymous
= 1;
1981 copy_HostAddresses(b
->addresses
, r
->et
.caddr
);
1984 r
->et
.transited
.tr_type
= DOMAIN_X500_COMPRESS
;
1985 krb5_data_zero(&r
->et
.transited
.contents
);
1987 /* The MIT ASN.1 library (obviously) doesn't tell lengths encoded
1988 * as 0 and as 0x80 (meaning indefinite length) apart, and is thus
1989 * incapable of correctly decoding SEQUENCE OF's of zero length.
1991 * To fix this, always send at least one no-op last_req
1993 * If there's a pw_end or valid_end we will use that,
1994 * otherwise just a dummy lr.
1996 r
->ek
.last_req
.val
= malloc(2 * sizeof(*r
->ek
.last_req
.val
));
1997 if (r
->ek
.last_req
.val
== NULL
) {
2001 r
->ek
.last_req
.len
= 0;
2002 if (r
->client
->entry
.pw_end
2003 && (config
->kdc_warn_pwexpire
== 0
2004 || kdc_time
+ config
->kdc_warn_pwexpire
>= *r
->client
->entry
.pw_end
)) {
2005 r
->ek
.last_req
.val
[r
->ek
.last_req
.len
].lr_type
= LR_PW_EXPTIME
;
2006 r
->ek
.last_req
.val
[r
->ek
.last_req
.len
].lr_value
= *r
->client
->entry
.pw_end
;
2007 ++r
->ek
.last_req
.len
;
2009 if (r
->client
->entry
.valid_end
) {
2010 r
->ek
.last_req
.val
[r
->ek
.last_req
.len
].lr_type
= LR_ACCT_EXPTIME
;
2011 r
->ek
.last_req
.val
[r
->ek
.last_req
.len
].lr_value
= *r
->client
->entry
.valid_end
;
2012 ++r
->ek
.last_req
.len
;
2014 if (r
->ek
.last_req
.len
== 0) {
2015 r
->ek
.last_req
.val
[r
->ek
.last_req
.len
].lr_type
= LR_NONE
;
2016 r
->ek
.last_req
.val
[r
->ek
.last_req
.len
].lr_value
= 0;
2017 ++r
->ek
.last_req
.len
;
2019 r
->ek
.nonce
= b
->nonce
;
2020 if (r
->client
->entry
.valid_end
|| r
->client
->entry
.pw_end
) {
2021 ALLOC(r
->ek
.key_expiration
);
2022 if (r
->client
->entry
.valid_end
) {
2023 if (r
->client
->entry
.pw_end
)
2024 *r
->ek
.key_expiration
= min(*r
->client
->entry
.valid_end
,
2025 *r
->client
->entry
.pw_end
);
2027 *r
->ek
.key_expiration
= *r
->client
->entry
.valid_end
;
2029 *r
->ek
.key_expiration
= *r
->client
->entry
.pw_end
;
2031 r
->ek
.key_expiration
= NULL
;
2032 r
->ek
.flags
= r
->et
.flags
;
2033 r
->ek
.authtime
= r
->et
.authtime
;
2034 if (r
->et
.starttime
) {
2035 ALLOC(r
->ek
.starttime
);
2036 *r
->ek
.starttime
= *r
->et
.starttime
;
2038 r
->ek
.endtime
= r
->et
.endtime
;
2039 if (r
->et
.renew_till
) {
2040 ALLOC(r
->ek
.renew_till
);
2041 *r
->ek
.renew_till
= *r
->et
.renew_till
;
2043 copy_Realm(&rep
.ticket
.realm
, &r
->ek
.srealm
);
2044 copy_PrincipalName(&rep
.ticket
.sname
, &r
->ek
.sname
);
2047 copy_HostAddresses(r
->et
.caddr
, r
->ek
.caddr
);
2051 * Check and session and reply keys
2054 if (r
->session_key
.keytype
== ETYPE_NULL
) {
2055 ret
= krb5_generate_random_keyblock(context
, r
->sessionetype
, &r
->session_key
);
2060 if (r
->reply_key
.keytype
== ETYPE_NULL
) {
2061 _kdc_set_e_text(r
, "Client have no reply key");
2062 ret
= KRB5KDC_ERR_CLIENT_NOTYET
;
2066 ret
= copy_EncryptionKey(&r
->session_key
, &r
->et
.key
);
2070 ret
= copy_EncryptionKey(&r
->session_key
, &r
->ek
.key
);
2075 * Add signing of alias referral
2078 if (f
.canonicalize
) {
2079 PA_ClientCanonicalized canon
;
2082 krb5_crypto cryptox
;
2085 memset(&canon
, 0, sizeof(canon
));
2087 canon
.names
.requested_name
= *b
->cname
;
2088 canon
.names
.mapped_name
= r
->client
->entry
.principal
->name
;
2090 ASN1_MALLOC_ENCODE(PA_ClientCanonicalizedNames
, data
.data
, data
.length
,
2091 &canon
.names
, &len
, ret
);
2094 if (data
.length
!= len
)
2095 krb5_abortx(context
, "internal asn.1 error");
2097 /* sign using "returned session key" */
2098 ret
= krb5_crypto_init(context
, &r
->et
.key
, 0, &cryptox
);
2104 ret
= krb5_create_checksum(context
, cryptox
,
2105 KRB5_KU_CANONICALIZED_NAMES
, 0,
2106 data
.data
, data
.length
,
2107 &canon
.canon_checksum
);
2109 krb5_crypto_destroy(context
, cryptox
);
2113 ASN1_MALLOC_ENCODE(PA_ClientCanonicalized
, data
.data
, data
.length
,
2115 free_Checksum(&canon
.canon_checksum
);
2118 if (data
.length
!= len
)
2119 krb5_abortx(context
, "internal asn.1 error");
2121 tmppa
.padata_type
= KRB5_PADATA_CLIENT_CANONICALIZED
;
2122 tmppa
.padata_value
= data
;
2123 ret
= add_METHOD_DATA(&r
->outpadata
, &tmppa
);
2129 if (r
->outpadata
.len
) {
2132 if (rep
.padata
== NULL
) {
2136 ret
= copy_METHOD_DATA(&r
->outpadata
, rep
.padata
);
2142 if (send_pac_p(context
, req
)) {
2143 generate_pac(r
, skey
);
2146 _kdc_log_timestamp(context
, config
, "AS-REQ", r
->et
.authtime
, r
->et
.starttime
,
2147 r
->et
.endtime
, r
->et
.renew_till
);
2149 /* do this as the last thing since this signs the EncTicketPart */
2150 ret
= _kdc_add_KRB5SignedPath(context
,
2154 r
->client
->entry
.principal
,
2161 log_as_req(context
, config
, r
->reply_key
.keytype
, setype
, b
);
2164 * We always say we support FAST/enc-pa-rep
2167 r
->et
.flags
.enc_pa_rep
= r
->ek
.flags
.enc_pa_rep
= 1;
2170 * Add REQ_ENC_PA_REP if client supports it
2174 pa
= _kdc_find_padata(req
, &i
, KRB5_PADATA_REQ_ENC_PA_REP
);
2177 ret
= add_enc_pa_rep(r
);
2179 const char *msg
= krb5_get_error_message(r
->context
, ret
);
2180 _kdc_r_log(r
, 0, "add_enc_pa_rep failed: %s: %d", msg
, ret
);
2181 krb5_free_error_message(r
->context
, msg
);
2190 ret
= _kdc_encode_reply(context
, config
,
2191 r
->armor_crypto
, req
->req_body
.nonce
,
2192 &rep
, &r
->et
, &r
->ek
, setype
, r
->server
->entry
.kvno
,
2193 &skey
->key
, r
->client
->entry
.kvno
,
2194 &r
->reply_key
, 0, &r
->e_text
, reply
);
2199 * Check if message too large
2201 if (datagram_reply
&& reply
->length
> config
->max_datagram_reply_length
) {
2202 krb5_data_free(reply
);
2203 ret
= KRB5KRB_ERR_RESPONSE_TOO_BIG
;
2204 _kdc_set_e_text(r
, "Reply packet too large");
2211 * In case of a non proxy error, build an error message.
2213 if(ret
!= 0 && ret
!= HDB_ERR_NOT_FOUND_HERE
) {
2214 ret
= _kdc_fast_mk_error(context
, r
,
2219 r
->client_princ
, r
->server_princ
,
2226 free_EncTicketPart(&r
->et
);
2227 free_EncKDCRepPart(&r
->ek
);
2228 free_KDCFastState(&r
->fast
);
2230 if (error_method
.len
)
2231 free_METHOD_DATA(&error_method
);
2232 if (r
->outpadata
.len
)
2233 free_METHOD_DATA(&r
->outpadata
);
2234 if (r
->client_princ
) {
2235 krb5_free_principal(context
, r
->client_princ
);
2236 r
->client_princ
= NULL
;
2238 if (r
->client_name
) {
2239 free(r
->client_name
);
2240 r
->client_name
= NULL
;
2242 if (r
->server_princ
){
2243 krb5_free_principal(context
, r
->server_princ
);
2244 r
->server_princ
= NULL
;
2246 if (r
->server_name
) {
2247 free(r
->server_name
);
2248 r
->server_name
= NULL
;
2251 _kdc_free_ent(context
, r
->client
);
2253 _kdc_free_ent(context
, r
->server
);
2254 if (r
->armor_crypto
) {
2255 krb5_crypto_destroy(r
->context
, r
->armor_crypto
);
2256 r
->armor_crypto
= NULL
;
2258 krb5_free_keyblock_contents(r
->context
, &r
->reply_key
);
2259 krb5_free_keyblock_contents(r
->context
, &r
->session_key
);
2264 * Add the AuthorizationData `data´ of `type´ to the last element in
2265 * the sequence of authorization_data in `tkt´ wrapped in an IF_RELEVANT
2269 _kdc_tkt_add_if_relevant_ad(krb5_context context
,
2272 const krb5_data
*data
)
2274 krb5_error_code ret
;
2277 if (tkt
->authorization_data
== NULL
) {
2278 tkt
->authorization_data
= calloc(1, sizeof(*tkt
->authorization_data
));
2279 if (tkt
->authorization_data
== NULL
) {
2280 krb5_set_error_message(context
, ENOMEM
, "out of memory");
2285 /* add the entry to the last element */
2287 AuthorizationData ad
= { 0, NULL
};
2288 AuthorizationDataElement ade
;
2291 ade
.ad_data
= *data
;
2293 ret
= add_AuthorizationData(&ad
, &ade
);
2295 krb5_set_error_message(context
, ret
, "add AuthorizationData failed");
2299 ade
.ad_type
= KRB5_AUTHDATA_IF_RELEVANT
;
2301 ASN1_MALLOC_ENCODE(AuthorizationData
,
2302 ade
.ad_data
.data
, ade
.ad_data
.length
,
2304 free_AuthorizationData(&ad
);
2306 krb5_set_error_message(context
, ret
, "ASN.1 encode of "
2307 "AuthorizationData failed");
2310 if (ade
.ad_data
.length
!= size
)
2311 krb5_abortx(context
, "internal asn.1 encoder error");
2313 ret
= add_AuthorizationData(tkt
->authorization_data
, &ade
);
2314 der_free_octet_string(&ade
.ad_data
);
2316 krb5_set_error_message(context
, ret
, "add AuthorizationData failed");