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
= 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
= (krb5_enctype
)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
);
163 p
[i
] != (krb5_enctype
)ETYPE_NULL
&& enctype
== (krb5_enctype
)ETYPE_NULL
;
165 if (krb5_enctype_valid(context
, p
[i
]) != 0)
168 /* check that the client supports it too */
169 for (j
= 0; j
< len
&& enctype
== (krb5_enctype
)ETYPE_NULL
; j
++) {
170 if (p
[i
] != etypes
[j
])
172 /* save best of union of { client, crypto system } */
173 if (clientbest
== (krb5_enctype
)ETYPE_NULL
)
175 /* check target princ support */
176 ret
= hdb_enctype2key(context
, &princ
->entry
, NULL
, p
[i
], &key
);
179 if (is_preauth
&& !is_default_salt_p(&def_salt
, key
))
184 if (clientbest
!= (krb5_enctype
)ETYPE_NULL
&&
185 enctype
== (krb5_enctype
)ETYPE_NULL
)
186 enctype
= clientbest
;
187 else if (enctype
== (krb5_enctype
)ETYPE_NULL
)
188 ret
= KRB5KDC_ERR_ETYPE_NOSUPP
;
189 if (ret
== 0 && ret_enctype
!= NULL
)
190 *ret_enctype
= enctype
;
191 if (ret
== 0 && ret_key
!= NULL
)
195 * Pick the first key from the client's enctype list that is
196 * supported by the cryptosystem and by the given principal.
198 * RFC4120 says we SHOULD pick the first _strong_ key from the
199 * client's list... not the first key... If the admin disallows
200 * weak enctypes in krb5.conf and selects this key selection
201 * algorithm, then we get exactly what RFC4120 says.
203 for(key
= NULL
, i
= 0; ret
!= 0 && i
< len
; i
++, key
= NULL
) {
205 if (krb5_enctype_valid(context
, etypes
[i
]) != 0 &&
206 !_kdc_is_weak_exception(princ
->entry
.principal
, etypes
[i
]))
209 while (hdb_next_enctype2key(context
, &princ
->entry
, NULL
,
210 etypes
[i
], &key
) == 0) {
211 if (key
->key
.keyvalue
.length
== 0) {
212 ret
= KRB5KDC_ERR_NULL_KEY
;
217 if (ret_enctype
!= NULL
)
218 *ret_enctype
= etypes
[i
];
220 if (is_preauth
&& is_default_salt_p(&def_salt
, key
))
227 krb5_free_salt (context
, def_salt
);
232 _kdc_make_anonymous_principalname (PrincipalName
*pn
)
234 pn
->name_type
= KRB5_NT_PRINCIPAL
;
235 pn
->name_string
.len
= 1;
236 pn
->name_string
.val
= malloc(sizeof(*pn
->name_string
.val
));
237 if (pn
->name_string
.val
== NULL
)
239 pn
->name_string
.val
[0] = strdup("anonymous");
240 if (pn
->name_string
.val
[0] == NULL
) {
241 free(pn
->name_string
.val
);
242 pn
->name_string
.val
= NULL
;
249 _kdc_r_log(kdc_request_t r
, int level
, const char *fmt
, ...)
254 s
= kdc_log_msg_va(r
->context
, r
->config
, level
, fmt
, ap
);
260 _kdc_set_e_text(kdc_request_t r
, const char *e_text
)
263 kdc_log(r
->context
, r
->config
, 0, "%s", e_text
);
267 _kdc_log_timestamp(krb5_context context
,
268 krb5_kdc_configuration
*config
,
270 KerberosTime authtime
, KerberosTime
*starttime
,
271 KerberosTime endtime
, KerberosTime
*renew_till
)
273 char authtime_str
[100], starttime_str
[100],
274 endtime_str
[100], renewtime_str
[100];
276 krb5_format_time(context
, authtime
,
277 authtime_str
, sizeof(authtime_str
), TRUE
);
279 krb5_format_time(context
, *starttime
,
280 starttime_str
, sizeof(starttime_str
), TRUE
);
282 strlcpy(starttime_str
, "unset", sizeof(starttime_str
));
283 krb5_format_time(context
, endtime
,
284 endtime_str
, sizeof(endtime_str
), TRUE
);
286 krb5_format_time(context
, *renew_till
,
287 renewtime_str
, sizeof(renewtime_str
), TRUE
);
289 strlcpy(renewtime_str
, "unset", sizeof(renewtime_str
));
291 kdc_log(context
, config
, 5,
292 "%s authtime: %s starttime: %s endtime: %s renew till: %s",
293 type
, authtime_str
, starttime_str
, endtime_str
, renewtime_str
);
302 static krb5_error_code
303 pa_pkinit_validate(kdc_request_t r
, const PA_DATA
*pa
)
305 pk_client_params
*pkp
= NULL
;
306 char *client_cert
= NULL
;
309 ret
= _kdc_pk_rd_padata(r
->context
, r
->config
, &r
->req
, pa
, r
->client
, &pkp
);
310 if (ret
|| pkp
== NULL
) {
311 ret
= KRB5KRB_AP_ERR_BAD_INTEGRITY
;
312 _kdc_r_log(r
, 5, "Failed to decode PKINIT PA-DATA -- %s",
317 ret
= _kdc_pk_check_client(r
->context
,
324 _kdc_set_e_text(r
, "PKINIT certificate not allowed to "
325 "impersonate principal");
329 _kdc_r_log(r
, 0, "PKINIT pre-authentication succeeded -- %s using %s",
330 r
->client_name
, client_cert
);
333 ret
= _kdc_pk_mk_pa_reply(r
->context
, r
->config
, pkp
, r
->client
,
334 r
->sessionetype
, &r
->req
, &r
->request
,
335 &r
->reply_key
, &r
->session_key
, &r
->outpadata
);
337 _kdc_set_e_text(r
, "Failed to build PK-INIT reply");
341 ret
= _kdc_add_inital_verified_cas(r
->context
, r
->config
,
346 _kdc_pk_free_client_param(r
->context
, pkp
);
357 static krb5_error_code
358 make_pa_enc_challange(krb5_context context
, METHOD_DATA
*md
,
365 EncryptedData encdata
;
370 krb5_us_timeofday (context
, &p
.patimestamp
, &usec
);
374 ASN1_MALLOC_ENCODE(PA_ENC_TS_ENC
, buf
, buf_size
, &p
, &len
, ret
);
378 krb5_abortx(context
, "internal error in ASN.1 encoder");
380 ret
= krb5_encrypt_EncryptedData(context
,
382 KRB5_KU_ENC_CHALLENGE_KDC
,
391 ASN1_MALLOC_ENCODE(EncryptedData
, buf
, buf_size
, &encdata
, &len
, ret
);
392 free_EncryptedData(&encdata
);
396 krb5_abortx(context
, "internal error in ASN.1 encoder");
398 ret
= krb5_padata_add(context
, md
, KRB5_PADATA_ENCRYPTED_CHALLENGE
, buf
, len
);
404 static krb5_error_code
405 pa_enc_chal_validate(kdc_request_t r
, const PA_DATA
*pa
)
407 krb5_data pepper1
, pepper2
, ts_data
;
408 KDC_REQ_BODY
*b
= &r
->req
.req_body
;
409 EncryptedData enc_data
;
410 krb5_enctype aenctype
;
416 heim_assert(r
->armor_crypto
!= NULL
, "ENC-CHAL called for non FAST");
418 if (b
->kdc_options
.request_anonymous
) {
419 ret
= KRB5KRB_AP_ERR_BAD_INTEGRITY
;
420 kdc_log(r
->context
, r
->config
, 0, "ENC-CHALL doesn't support anon");
424 ret
= decode_EncryptedData(pa
->padata_value
.data
,
425 pa
->padata_value
.length
,
429 ret
= KRB5KRB_AP_ERR_BAD_INTEGRITY
;
430 _kdc_r_log(r
, 5, "Failed to decode PA-DATA -- %s",
435 pepper1
.data
= "clientchallengearmor";
436 pepper1
.length
= strlen(pepper1
.data
);
437 pepper2
.data
= "challengelongterm";
438 pepper2
.length
= strlen(pepper2
.data
);
440 krb5_crypto_getenctype(r
->context
, r
->armor_crypto
, &aenctype
);
442 for (i
= 0; i
< r
->client
->entry
.keys
.len
; i
++) {
443 krb5_crypto challangecrypto
, longtermcrypto
;
444 krb5_keyblock challangekey
;
447 k
= &r
->client
->entry
.keys
.val
[i
];
449 ret
= krb5_crypto_init(r
->context
, &k
->key
, 0, &longtermcrypto
);
453 ret
= krb5_crypto_fx_cf2(r
->context
, r
->armor_crypto
, longtermcrypto
,
454 &pepper1
, &pepper2
, aenctype
,
456 krb5_crypto_destroy(r
->context
, longtermcrypto
);
460 ret
= krb5_crypto_init(r
->context
, &challangekey
, 0,
465 ret
= krb5_decrypt_EncryptedData(r
->context
, challangecrypto
,
466 KRB5_KU_ENC_CHALLENGE_CLIENT
,
472 ret
= decode_PA_ENC_TS_ENC(ts_data
.data
,
476 krb5_data_free(&ts_data
);
478 krb5_crypto_destroy(r
->context
, challangecrypto
);
479 ret
= KRB5KDC_ERR_PREAUTH_FAILED
;
480 _kdc_r_log(r
, 5, "Failed to decode PA-ENC-TS_ENC -- %s",
485 if (abs(kdc_time
- p
.patimestamp
) > r
->context
->max_skew
) {
486 char client_time
[100];
488 krb5_crypto_destroy(r
->context
, challangecrypto
);
490 krb5_format_time(r
->context
, p
.patimestamp
,
491 client_time
, sizeof(client_time
), TRUE
);
493 ret
= KRB5KRB_AP_ERR_SKEW
;
494 _kdc_r_log(r
, 0, "Too large time skew, "
495 "client time %s is out by %u > %u seconds -- %s",
497 (unsigned)abs(kdc_time
- p
.patimestamp
),
498 r
->context
->max_skew
,
501 free_PA_ENC_TS_ENC(&p
);
505 free_PA_ENC_TS_ENC(&p
);
507 ret
= make_pa_enc_challange(r
->context
, &r
->outpadata
,
509 krb5_crypto_destroy(r
->context
, challangecrypto
);
513 set_salt_padata(&r
->outpadata
, k
->salt
);
514 krb5_free_keyblock_contents(r
->context
, &r
->reply_key
);
515 ret
= krb5_copy_keyblock_contents(r
->context
, &k
->key
, &r
->reply_key
);
519 if (i
< r
->client
->entry
.keys
.len
)
520 ret
= KRB5KDC_ERR_PREAUTH_FAILED
;
522 free_EncryptedData(&enc_data
);
527 static krb5_error_code
528 pa_enc_ts_validate(kdc_request_t r
, const PA_DATA
*pa
)
530 EncryptedData enc_data
;
539 if (r
->req
.req_body
.kdc_options
.request_anonymous
) {
540 ret
= KRB5KRB_AP_ERR_BAD_INTEGRITY
;
541 _kdc_set_e_text(r
, "ENC-TS doesn't suport anon");
545 ret
= decode_EncryptedData(pa
->padata_value
.data
,
546 pa
->padata_value
.length
,
550 ret
= KRB5KRB_AP_ERR_BAD_INTEGRITY
;
551 _kdc_r_log(r
, 5, "Failed to decode PA-DATA -- %s",
556 ret
= hdb_enctype2key(r
->context
, &r
->client
->entry
, NULL
,
557 enc_data
.etype
, &pa_key
);
560 _kdc_set_e_text(r
, "No key matching entype");
561 ret
= KRB5KDC_ERR_ETYPE_NOSUPP
;
562 if(krb5_enctype_to_string(r
->context
, enc_data
.etype
, &estr
))
566 "No client key matching pa-data (%d) -- %s",
567 enc_data
.etype
, r
->client_name
);
570 "No client key matching pa-data (%s) -- %s",
571 estr
, r
->client_name
);
573 free_EncryptedData(&enc_data
);
578 ret
= krb5_crypto_init(r
->context
, &pa_key
->key
, 0, &crypto
);
580 const char *msg
= krb5_get_error_message(r
->context
, ret
);
581 _kdc_r_log(r
, 0, "krb5_crypto_init failed: %s", msg
);
582 krb5_free_error_message(r
->context
, msg
);
583 free_EncryptedData(&enc_data
);
587 ret
= krb5_decrypt_EncryptedData (r
->context
,
589 KRB5_KU_PA_ENC_TIMESTAMP
,
592 krb5_crypto_destroy(r
->context
, crypto
);
594 * Since the user might have several keys with the same
595 * enctype but with diffrent salting, we need to try all
596 * the keys with the same enctype.
599 krb5_error_code ret2
;
600 const char *msg
= krb5_get_error_message(r
->context
, ret
);
602 ret2
= krb5_enctype_to_string(r
->context
,
603 pa_key
->key
.keytype
, &str
);
606 _kdc_r_log(r
, 5, "Failed to decrypt PA-DATA -- %s "
607 "(enctype %s) error %s",
608 r
->client_name
, str
? str
: "unknown enctype", msg
);
609 krb5_free_error_message(r
->context
, msg
);
612 if(hdb_next_enctype2key(r
->context
, &r
->client
->entry
, NULL
,
613 enc_data
.etype
, &pa_key
) == 0)
616 free_EncryptedData(&enc_data
);
618 if (r
->clientdb
->hdb_auth_status
)
619 r
->clientdb
->hdb_auth_status(r
->context
, r
->clientdb
, r
->client
,
620 HDB_AUTH_WRONG_PASSWORD
);
622 ret
= KRB5KDC_ERR_PREAUTH_FAILED
;
625 free_EncryptedData(&enc_data
);
626 ret
= decode_PA_ENC_TS_ENC(ts_data
.data
,
630 krb5_data_free(&ts_data
);
632 ret
= KRB5KDC_ERR_PREAUTH_FAILED
;
633 _kdc_r_log(r
, 5, "Failed to decode PA-ENC-TS_ENC -- %s",
637 if (abs(kdc_time
- p
.patimestamp
) > r
->context
->max_skew
) {
638 char client_time
[100];
640 krb5_format_time(r
->context
, p
.patimestamp
,
641 client_time
, sizeof(client_time
), TRUE
);
643 ret
= KRB5KRB_AP_ERR_SKEW
;
644 _kdc_r_log(r
, 0, "Too large time skew, "
645 "client time %s is out by %u > %u seconds -- %s",
647 (unsigned)abs(kdc_time
- p
.patimestamp
),
648 r
->context
->max_skew
,
652 * The following is needed to make windows clients to
653 * retry using the timestamp in the error message, if
654 * there is a e_text, they become unhappy.
657 free_PA_ENC_TS_ENC(&p
);
660 free_PA_ENC_TS_ENC(&p
);
662 set_salt_padata(&r
->outpadata
, pa_key
->salt
);
664 ret
= krb5_copy_keyblock_contents(r
->context
, &pa_key
->key
, &r
->reply_key
);
668 ret
= krb5_enctype_to_string(r
->context
, pa_key
->key
.keytype
, &str
);
671 _kdc_r_log(r
, 2, "ENC-TS Pre-authentication succeeded -- %s using %s",
672 r
->client_name
, str
? str
: "unknown enctype");
686 #define PA_ANNOUNCE 1
687 #define PA_REQ_FAST 2 /* only use inside fast */
688 krb5_error_code (*validate
)(kdc_request_t
, const PA_DATA
*pa
);
691 static const struct kdc_patypes pat
[] = {
694 KRB5_PADATA_PK_AS_REQ
, "PK-INIT(ietf)", PA_ANNOUNCE
,
698 KRB5_PADATA_PK_AS_REQ_WIN
, "PK-INIT(win2k)", PA_ANNOUNCE
,
702 { KRB5_PADATA_PK_AS_REQ
, "PK-INIT(ietf)", 0, NULL
},
703 { KRB5_PADATA_PK_AS_REQ_WIN
, "PK-INIT(win2k)", 0, NULL
},
705 { KRB5_PADATA_PA_PK_OCSP_RESPONSE
, "OCSP", 0, NULL
},
707 KRB5_PADATA_ENC_TIMESTAMP
, "ENC-TS",
712 KRB5_PADATA_ENCRYPTED_CHALLENGE
, "ENC-CHAL",
713 PA_ANNOUNCE
| PA_REQ_FAST
,
716 { KRB5_PADATA_REQ_ENC_PA_REP
, "REQ-ENC-PA-REP", 0, NULL
},
717 { KRB5_PADATA_FX_FAST
, "FX-FAST", PA_ANNOUNCE
, NULL
},
718 { KRB5_PADATA_FX_ERROR
, "FX-ERROR", 0, NULL
},
719 { KRB5_PADATA_FX_COOKIE
, "FX-COOKIE", 0, NULL
}
723 log_patypes(krb5_context context
,
724 krb5_kdc_configuration
*config
,
727 struct rk_strpool
*p
= NULL
;
731 for (n
= 0; n
< padata
->len
; n
++) {
732 for (m
= 0; m
< sizeof(pat
) / sizeof(pat
[0]); m
++) {
733 if (padata
->val
[n
].padata_type
== pat
[m
].type
) {
734 p
= rk_strpoolprintf(p
, "%s", pat
[m
].name
);
738 if (m
== sizeof(pat
) / sizeof(pat
[0]))
739 p
= rk_strpoolprintf(p
, "%d", padata
->val
[n
].padata_type
);
740 if (p
&& n
+ 1 < padata
->len
)
741 p
= rk_strpoolprintf(p
, ", ");
743 kdc_log(context
, config
, 0, "out of memory");
748 p
= rk_strpoolprintf(p
, "none");
750 str
= rk_strpoolcollect(p
);
751 kdc_log(context
, config
, 0, "Client sent patypes: %s", str
);
760 _kdc_encode_reply(krb5_context context
,
761 krb5_kdc_configuration
*config
,
762 krb5_crypto armor_crypto
, uint32_t nonce
,
763 KDC_REP
*rep
, EncTicketPart
*et
, EncKDCRepPart
*ek
,
765 int skvno
, const EncryptionKey
*skey
,
766 int ckvno
, const EncryptionKey
*reply_key
,
777 ASN1_MALLOC_ENCODE(EncTicketPart
, buf
, buf_size
, et
, &len
, ret
);
779 const char *msg
= krb5_get_error_message(context
, ret
);
780 kdc_log(context
, config
, 0, "Failed to encode ticket: %s", msg
);
781 krb5_free_error_message(context
, msg
);
785 krb5_abortx(context
, "Internal error in ASN.1 encoder");
787 ret
= krb5_crypto_init(context
, skey
, etype
, &crypto
);
789 const char *msg
= krb5_get_error_message(context
, ret
);
790 kdc_log(context
, config
, 0, "krb5_crypto_init failed: %s", msg
);
791 krb5_free_error_message(context
, msg
);
795 ret
= krb5_encrypt_EncryptedData(context
,
801 &rep
->ticket
.enc_part
);
803 krb5_crypto_destroy(context
, crypto
);
805 const char *msg
= krb5_get_error_message(context
, ret
);
806 kdc_log(context
, config
, 0, "Failed to encrypt data: %s", msg
);
807 krb5_free_error_message(context
, msg
);
813 krb5_keyblock
*strengthen_key
= NULL
;
814 KrbFastFinished finished
;
816 kdc_log(context
, config
, 0, "FAST armor protection");
818 memset(&finished
, 0, sizeof(finished
));
819 krb5_data_zero(&data
);
821 finished
.timestamp
= kdc_time
;
823 finished
.crealm
= et
->crealm
;
824 finished
.cname
= et
->cname
;
826 ASN1_MALLOC_ENCODE(Ticket
, data
.data
, data
.length
,
827 &rep
->ticket
, &len
, ret
);
830 if (data
.length
!= len
)
831 krb5_abortx(context
, "internal asn.1 error");
833 ret
= krb5_create_checksum(context
, armor_crypto
,
834 KRB5_KU_FAST_FINISHED
, 0,
835 data
.data
, data
.length
,
836 &finished
.ticket_checksum
);
837 krb5_data_free(&data
);
841 ret
= _kdc_fast_mk_response(context
, armor_crypto
,
842 rep
->padata
, strengthen_key
, &finished
,
844 free_Checksum(&finished
.ticket_checksum
);
849 free_METHOD_DATA(rep
->padata
);
851 rep
->padata
= calloc(1, sizeof(*(rep
->padata
)));
852 if (rep
->padata
== NULL
) {
853 krb5_data_free(&data
);
858 ret
= krb5_padata_add(context
, rep
->padata
,
860 data
.data
, data
.length
);
865 * Hide client name of privacy reasons
867 if (1 /* r->fast_options.hide_client_names */) {
868 rep
->crealm
[0] = '\0';
869 free_PrincipalName(&rep
->cname
);
870 rep
->cname
.name_type
= 0;
874 if(rep
->msg_type
== krb_as_rep
&& !config
->encode_as_rep_as_tgs_rep
)
875 ASN1_MALLOC_ENCODE(EncASRepPart
, buf
, buf_size
, ek
, &len
, ret
);
877 ASN1_MALLOC_ENCODE(EncTGSRepPart
, buf
, buf_size
, ek
, &len
, ret
);
879 const char *msg
= krb5_get_error_message(context
, ret
);
880 kdc_log(context
, config
, 0, "Failed to encode KDC-REP: %s", msg
);
881 krb5_free_error_message(context
, msg
);
884 if(buf_size
!= len
) {
886 kdc_log(context
, config
, 0, "Internal error in ASN.1 encoder");
887 *e_text
= "KDC internal error";
888 return KRB5KRB_ERR_GENERIC
;
890 ret
= krb5_crypto_init(context
, reply_key
, 0, &crypto
);
892 const char *msg
= krb5_get_error_message(context
, ret
);
894 kdc_log(context
, config
, 0, "krb5_crypto_init failed: %s", msg
);
895 krb5_free_error_message(context
, msg
);
898 if(rep
->msg_type
== krb_as_rep
) {
899 krb5_encrypt_EncryptedData(context
,
901 KRB5_KU_AS_REP_ENC_PART
,
907 ASN1_MALLOC_ENCODE(AS_REP
, buf
, buf_size
, rep
, &len
, ret
);
909 krb5_encrypt_EncryptedData(context
,
911 rk_is_subkey
? KRB5_KU_TGS_REP_ENC_PART_SUB_KEY
: KRB5_KU_TGS_REP_ENC_PART_SESSION
,
917 ASN1_MALLOC_ENCODE(TGS_REP
, buf
, buf_size
, rep
, &len
, ret
);
919 krb5_crypto_destroy(context
, crypto
);
921 const char *msg
= krb5_get_error_message(context
, ret
);
922 kdc_log(context
, config
, 0, "Failed to encode KDC-REP: %s", msg
);
923 krb5_free_error_message(context
, msg
);
926 if(buf_size
!= len
) {
928 kdc_log(context
, config
, 0, "Internal error in ASN.1 encoder");
929 *e_text
= "KDC internal error";
930 return KRB5KRB_ERR_GENERIC
;
933 reply
->length
= buf_size
;
938 * Return 1 if the client have only older enctypes, this is for
939 * determining if the server should send ETYPE_INFO2 or not.
943 older_enctype(krb5_enctype enctype
)
946 case ETYPE_DES_CBC_CRC
:
947 case ETYPE_DES_CBC_MD4
:
948 case ETYPE_DES_CBC_MD5
:
949 case ETYPE_DES3_CBC_SHA1
:
950 case ETYPE_ARCFOUR_HMAC_MD5
:
951 case ETYPE_ARCFOUR_HMAC_MD5_56
:
953 * The following three is "old" windows enctypes and is needed for
954 * windows 2000 hosts.
956 case ETYPE_ARCFOUR_MD4
:
957 case ETYPE_ARCFOUR_HMAC_OLD
:
958 case ETYPE_ARCFOUR_HMAC_OLD_EXP
:
969 static krb5_error_code
970 make_etype_info_entry(krb5_context context
, ETYPE_INFO_ENTRY
*ent
, Key
*key
)
972 ent
->etype
= key
->key
.keytype
;
975 ALLOC(ent
->salttype
);
977 if(key
->salt
->type
== hdb_pw_salt
)
978 *ent
->salttype
= 0; /* or 1? or NULL? */
979 else if(key
->salt
->type
== hdb_afs3_salt
)
982 kdc_log(context
, config
, 0, "unknown salt-type: %d",
984 return KRB5KRB_ERR_GENERIC
;
986 /* according to `the specs', we can't send a salt if
987 we have AFS3 salted key, but that requires that you
988 *know* what cell you are using (e.g by assuming
989 that the cell is the same as the realm in lower
992 ALLOC(ent
->salttype
);
993 *ent
->salttype
= key
->salt
->type
;
996 * We shouldn't sent salttype since it is incompatible with the
997 * specification and it breaks windows clients. The afs
998 * salting problem is solved by using KRB5-PADATA-AFS3-SALT
999 * implemented in Heimdal 0.7 and later.
1001 ent
->salttype
= NULL
;
1003 krb5_copy_data(context
, &key
->salt
->salt
,
1006 /* we return no salt type at all, as that should indicate
1007 * the default salt type and make everybody happy. some
1008 * systems (like w2k) dislike being told the salt type
1011 ent
->salttype
= NULL
;
1017 static krb5_error_code
1018 get_pa_etype_info(krb5_context context
,
1019 krb5_kdc_configuration
*config
,
1020 METHOD_DATA
*md
, Key
*ckey
)
1022 krb5_error_code ret
= 0;
1029 pa
.val
= calloc(1, sizeof(pa
.val
[0]));
1033 ret
= make_etype_info_entry(context
, &pa
.val
[0], ckey
);
1035 free_ETYPE_INFO(&pa
);
1039 ASN1_MALLOC_ENCODE(ETYPE_INFO
, buf
, len
, &pa
, &len
, ret
);
1040 free_ETYPE_INFO(&pa
);
1043 ret
= realloc_method_data(md
);
1048 md
->val
[md
->len
- 1].padata_type
= KRB5_PADATA_ETYPE_INFO
;
1049 md
->val
[md
->len
- 1].padata_value
.length
= len
;
1050 md
->val
[md
->len
- 1].padata_value
.data
= buf
;
1058 extern int _krb5_AES_string_to_default_iterator
;
1060 static krb5_error_code
1061 make_etype_info2_entry(ETYPE_INFO2_ENTRY
*ent
, Key
*key
)
1063 ent
->etype
= key
->key
.keytype
;
1066 if (ent
->salt
== NULL
)
1068 *ent
->salt
= malloc(key
->salt
->salt
.length
+ 1);
1069 if (*ent
->salt
== NULL
) {
1074 memcpy(*ent
->salt
, key
->salt
->salt
.data
, key
->salt
->salt
.length
);
1075 (*ent
->salt
)[key
->salt
->salt
.length
] = '\0';
1079 ent
->s2kparams
= NULL
;
1081 switch (key
->key
.keytype
) {
1082 case ETYPE_AES128_CTS_HMAC_SHA1_96
:
1083 case ETYPE_AES256_CTS_HMAC_SHA1_96
:
1084 ALLOC(ent
->s2kparams
);
1085 if (ent
->s2kparams
== NULL
)
1087 ent
->s2kparams
->length
= 4;
1088 ent
->s2kparams
->data
= malloc(ent
->s2kparams
->length
);
1089 if (ent
->s2kparams
->data
== NULL
) {
1090 free(ent
->s2kparams
);
1091 ent
->s2kparams
= NULL
;
1094 _krb5_put_int(ent
->s2kparams
->data
,
1095 _krb5_AES_string_to_default_iterator
,
1096 ent
->s2kparams
->length
);
1098 case ETYPE_DES_CBC_CRC
:
1099 case ETYPE_DES_CBC_MD4
:
1100 case ETYPE_DES_CBC_MD5
:
1101 /* Check if this was a AFS3 salted key */
1102 if(key
->salt
&& key
->salt
->type
== hdb_afs3_salt
){
1103 ALLOC(ent
->s2kparams
);
1104 if (ent
->s2kparams
== NULL
)
1106 ent
->s2kparams
->length
= 1;
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
,
1115 ent
->s2kparams
->length
);
1125 * Return an ETYPE-INFO2. Enctypes are storted the same way as in the
1126 * database (client supported enctypes first, then the unsupported
1130 static krb5_error_code
1131 get_pa_etype_info2(krb5_context context
,
1132 krb5_kdc_configuration
*config
,
1133 METHOD_DATA
*md
, Key
*ckey
)
1135 krb5_error_code ret
= 0;
1141 pa
.val
= calloc(1, sizeof(pa
.val
[0]));
1145 ret
= make_etype_info2_entry(&pa
.val
[0], ckey
);
1147 free_ETYPE_INFO2(&pa
);
1151 ASN1_MALLOC_ENCODE(ETYPE_INFO2
, buf
, len
, &pa
, &len
, ret
);
1152 free_ETYPE_INFO2(&pa
);
1155 ret
= realloc_method_data(md
);
1160 md
->val
[md
->len
- 1].padata_type
= KRB5_PADATA_ETYPE_INFO2
;
1161 md
->val
[md
->len
- 1].padata_value
.length
= len
;
1162 md
->val
[md
->len
- 1].padata_value
.data
= buf
;
1171 log_as_req(krb5_context context
,
1172 krb5_kdc_configuration
*config
,
1173 krb5_enctype cetype
,
1174 krb5_enctype setype
,
1175 const KDC_REQ_BODY
*b
)
1177 krb5_error_code ret
;
1178 struct rk_strpool
*p
;
1182 p
= rk_strpoolprintf(NULL
, "%s", "Client supported enctypes: ");
1184 for (i
= 0; i
< b
->etype
.len
; i
++) {
1185 ret
= krb5_enctype_to_string(context
, b
->etype
.val
[i
], &str
);
1187 p
= rk_strpoolprintf(p
, "%s", str
);
1190 p
= rk_strpoolprintf(p
, "%d", b
->etype
.val
[i
]);
1191 if (p
&& i
+ 1 < b
->etype
.len
)
1192 p
= rk_strpoolprintf(p
, ", ");
1194 kdc_log(context
, config
, 0, "out of memory");
1199 p
= rk_strpoolprintf(p
, "no encryption types");
1205 ret
= krb5_enctype_to_string(context
, cetype
, &cet
);
1207 ret
= krb5_enctype_to_string(context
, setype
, &set
);
1209 p
= rk_strpoolprintf(p
, ", using %s/%s", cet
, set
);
1215 p
= rk_strpoolprintf(p
, ", using enctypes %d/%d",
1219 str
= rk_strpoolcollect(p
);
1220 kdc_log(context
, config
, 0, "%s", str
);
1225 unparse_flags(KDCOptions2int(b
->kdc_options
), asn1_KDCOptions_units(),
1226 fixedstr
, sizeof(fixedstr
));
1228 kdc_log(context
, config
, 0, "Requested flags: %s", fixedstr
);
1233 * verify the flags on `client' and `server', returning 0
1234 * if they are OK and generating an error messages and returning
1235 * and error code otherwise.
1239 kdc_check_flags(krb5_context context
,
1240 krb5_kdc_configuration
*config
,
1241 hdb_entry_ex
*client_ex
, const char *client_name
,
1242 hdb_entry_ex
*server_ex
, const char *server_name
,
1243 krb5_boolean is_as_req
)
1245 if(client_ex
!= NULL
) {
1246 hdb_entry
*client
= &client_ex
->entry
;
1249 if (client
->flags
.locked_out
) {
1250 kdc_log(context
, config
, 0,
1251 "Client (%s) is locked out", client_name
);
1252 return KRB5KDC_ERR_POLICY
;
1255 if (client
->flags
.invalid
) {
1256 kdc_log(context
, config
, 0,
1257 "Client (%s) has invalid bit set", client_name
);
1258 return KRB5KDC_ERR_POLICY
;
1261 if(!client
->flags
.client
){
1262 kdc_log(context
, config
, 0,
1263 "Principal may not act as client -- %s", client_name
);
1264 return KRB5KDC_ERR_POLICY
;
1267 if (client
->valid_start
&& *client
->valid_start
> kdc_time
) {
1268 char starttime_str
[100];
1269 krb5_format_time(context
, *client
->valid_start
,
1270 starttime_str
, sizeof(starttime_str
), TRUE
);
1271 kdc_log(context
, config
, 0,
1272 "Client not yet valid until %s -- %s",
1273 starttime_str
, client_name
);
1274 return KRB5KDC_ERR_CLIENT_NOTYET
;
1277 if (client
->valid_end
&& *client
->valid_end
< kdc_time
) {
1278 char endtime_str
[100];
1279 krb5_format_time(context
, *client
->valid_end
,
1280 endtime_str
, sizeof(endtime_str
), TRUE
);
1281 kdc_log(context
, config
, 0,
1282 "Client expired at %s -- %s",
1283 endtime_str
, client_name
);
1284 return KRB5KDC_ERR_NAME_EXP
;
1287 if (client
->flags
.require_pwchange
&&
1288 (server_ex
== NULL
|| !server_ex
->entry
.flags
.change_pw
)) {
1289 kdc_log(context
, config
, 0,
1290 "Client's key must be changed -- %s", client_name
);
1291 return KRB5KDC_ERR_KEY_EXPIRED
;
1294 if (client
->pw_end
&& *client
->pw_end
< kdc_time
1295 && (server_ex
== NULL
|| !server_ex
->entry
.flags
.change_pw
)) {
1296 char pwend_str
[100];
1297 krb5_format_time(context
, *client
->pw_end
,
1298 pwend_str
, sizeof(pwend_str
), TRUE
);
1299 kdc_log(context
, config
, 0,
1300 "Client's key has expired at %s -- %s",
1301 pwend_str
, client_name
);
1302 return KRB5KDC_ERR_KEY_EXPIRED
;
1308 if (server_ex
!= NULL
) {
1309 hdb_entry
*server
= &server_ex
->entry
;
1311 if (server
->flags
.locked_out
) {
1312 kdc_log(context
, config
, 0,
1313 "Client server locked out -- %s", server_name
);
1314 return KRB5KDC_ERR_POLICY
;
1316 if (server
->flags
.invalid
) {
1317 kdc_log(context
, config
, 0,
1318 "Server has invalid flag set -- %s", server_name
);
1319 return KRB5KDC_ERR_POLICY
;
1322 if(!server
->flags
.server
){
1323 kdc_log(context
, config
, 0,
1324 "Principal may not act as server -- %s", server_name
);
1325 return KRB5KDC_ERR_POLICY
;
1328 if(!is_as_req
&& server
->flags
.initial
) {
1329 kdc_log(context
, config
, 0,
1330 "AS-REQ is required for server -- %s", server_name
);
1331 return KRB5KDC_ERR_POLICY
;
1334 if (server
->valid_start
&& *server
->valid_start
> kdc_time
) {
1335 char starttime_str
[100];
1336 krb5_format_time(context
, *server
->valid_start
,
1337 starttime_str
, sizeof(starttime_str
), TRUE
);
1338 kdc_log(context
, config
, 0,
1339 "Server not yet valid until %s -- %s",
1340 starttime_str
, server_name
);
1341 return KRB5KDC_ERR_SERVICE_NOTYET
;
1344 if (server
->valid_end
&& *server
->valid_end
< kdc_time
) {
1345 char endtime_str
[100];
1346 krb5_format_time(context
, *server
->valid_end
,
1347 endtime_str
, sizeof(endtime_str
), TRUE
);
1348 kdc_log(context
, config
, 0,
1349 "Server expired at %s -- %s",
1350 endtime_str
, server_name
);
1351 return KRB5KDC_ERR_SERVICE_EXP
;
1354 if (server
->pw_end
&& *server
->pw_end
< kdc_time
) {
1355 char pwend_str
[100];
1356 krb5_format_time(context
, *server
->pw_end
,
1357 pwend_str
, sizeof(pwend_str
), TRUE
);
1358 kdc_log(context
, config
, 0,
1359 "Server's key has expired at -- %s",
1360 pwend_str
, server_name
);
1361 return KRB5KDC_ERR_KEY_EXPIRED
;
1368 * Return TRUE if `from' is part of `addresses' taking into consideration
1369 * the configuration variables that tells us how strict we should be about
1374 _kdc_check_addresses(krb5_context context
,
1375 krb5_kdc_configuration
*config
,
1376 HostAddresses
*addresses
, const struct sockaddr
*from
)
1378 krb5_error_code ret
;
1380 krb5_boolean result
;
1381 krb5_boolean only_netbios
= TRUE
;
1384 if(config
->check_ticket_addresses
== 0)
1387 if(addresses
== NULL
)
1388 return config
->allow_null_ticket_addresses
;
1390 for (i
= 0; i
< addresses
->len
; ++i
) {
1391 if (addresses
->val
[i
].addr_type
!= KRB5_ADDRESS_NETBIOS
) {
1392 only_netbios
= FALSE
;
1396 /* Windows sends it's netbios name, which I can only assume is
1397 * used for the 'allowed workstations' check. This is painful,
1398 * but we still want to check IP addresses if they happen to be
1403 return config
->allow_null_ticket_addresses
;
1405 ret
= krb5_sockaddr2address (context
, from
, &addr
);
1409 result
= krb5_address_search(context
, &addr
, addresses
);
1410 krb5_free_address (context
, &addr
);
1419 send_pac_p(krb5_context context
, KDC_REQ
*req
)
1421 krb5_error_code ret
;
1422 PA_PAC_REQUEST pacreq
;
1426 pa
= _kdc_find_padata(req
, &i
, KRB5_PADATA_PA_PAC_REQUEST
);
1430 ret
= decode_PA_PAC_REQUEST(pa
->padata_value
.data
,
1431 pa
->padata_value
.length
,
1436 i
= pacreq
.include_pac
;
1437 free_PA_PAC_REQUEST(&pacreq
);
1447 static krb5_error_code
1448 generate_pac(kdc_request_t r
, Key
*skey
)
1450 krb5_error_code ret
;
1454 ret
= _kdc_pac_generate(r
->context
, r
->client
, &p
);
1456 _kdc_r_log(r
, 0, "PAC generation failed for -- %s",
1463 ret
= _krb5_pac_sign(r
->context
, p
, r
->et
.authtime
,
1464 r
->client
->entry
.principal
,
1465 &skey
->key
, /* Server key */
1466 &skey
->key
, /* FIXME: should be krbtgt key */
1468 krb5_pac_free(r
->context
, p
);
1470 _kdc_r_log(r
, 0, "PAC signing failed for -- %s",
1475 ret
= _kdc_tkt_add_if_relevant_ad(r
->context
, &r
->et
,
1476 KRB5_AUTHDATA_WIN2K_PAC
,
1478 krb5_data_free(&data
);
1488 _kdc_is_anonymous(krb5_context context
, krb5_principal principal
)
1490 if (principal
->name
.name_type
!= KRB5_NT_WELLKNOWN
||
1491 principal
->name
.name_string
.len
!= 2 ||
1492 strcmp(principal
->name
.name_string
.val
[0], KRB5_WELLKNOWN_NAME
) != 0 ||
1493 strcmp(principal
->name
.name_string
.val
[1], KRB5_ANON_NAME
) != 0)
1499 require_preauth_p(kdc_request_t r
)
1501 return r
->config
->require_preauth
1502 || r
->client
->entry
.flags
.require_preauth
1503 || r
->server
->entry
.flags
.require_preauth
;
1511 static krb5_error_code
1512 add_enc_pa_rep(kdc_request_t r
)
1514 krb5_error_code ret
;
1520 r
->et
.flags
.enc_pa_rep
= r
->ek
.flags
.enc_pa_rep
= 1;
1522 ret
= krb5_crypto_init(r
->context
, &r
->reply_key
, 0, &crypto
);
1526 ret
= krb5_create_checksum(r
->context
, crypto
,
1528 r
->request
.data
, r
->request
.length
,
1530 krb5_crypto_destroy(r
->context
, crypto
);
1534 ASN1_MALLOC_ENCODE(Checksum
, cdata
.data
, cdata
.length
,
1535 &checksum
, &len
, ret
);
1536 free_Checksum(&checksum
);
1539 heim_assert(cdata
.length
== len
, "ASN.1 internal error");
1541 if (r
->ek
.encrypted_pa_data
== NULL
) {
1542 ALLOC(r
->ek
.encrypted_pa_data
);
1543 if (r
->ek
.encrypted_pa_data
== NULL
)
1546 ret
= krb5_padata_add(r
->context
, r
->ek
.encrypted_pa_data
,
1547 KRB5_PADATA_REQ_ENC_PA_REP
, cdata
.data
, cdata
.length
);
1551 return krb5_padata_add(r
->context
, r
->ek
.encrypted_pa_data
,
1552 KRB5_PADATA_FX_FAST
, NULL
, 0);
1560 _kdc_as_rep(kdc_request_t r
,
1563 struct sockaddr
*from_addr
,
1566 krb5_context context
= r
->context
;
1567 krb5_kdc_configuration
*config
= r
->config
;
1568 KDC_REQ
*req
= &r
->req
;
1569 KDC_REQ_BODY
*b
= NULL
;
1572 krb5_enctype setype
;
1573 krb5_error_code ret
= 0;
1576 int i
, flags
= HDB_F_FOR_AS_REQ
;
1577 METHOD_DATA error_method
;
1580 memset(&rep
, 0, sizeof(rep
));
1581 error_method
.len
= 0;
1582 error_method
.val
= NULL
;
1585 * Look for FAST armor and unwrap
1587 ret
= _kdc_fast_unwrap_request(r
);
1589 _kdc_r_log(r
, 0, "FAST unwrap request from %s failed: %d", from
, ret
);
1597 flags
|= HDB_F_CANON
;
1599 if(b
->sname
== NULL
){
1600 ret
= KRB5KRB_ERR_GENERIC
;
1601 _kdc_set_e_text(r
, "No server in request");
1603 ret
= _krb5_principalname2krb5_principal (context
,
1608 ret
= krb5_unparse_name(context
, r
->server_princ
, &r
->server_name
);
1611 kdc_log(context
, config
, 0,
1612 "AS-REQ malformed server name from %s", from
);
1615 if(b
->cname
== NULL
){
1616 ret
= KRB5KRB_ERR_GENERIC
;
1617 _kdc_set_e_text(r
, "No client in request");
1619 ret
= _krb5_principalname2krb5_principal (context
,
1626 ret
= krb5_unparse_name(context
, r
->client_princ
, &r
->client_name
);
1629 kdc_log(context
, config
, 0,
1630 "AS-REQ malformed client name from %s", from
);
1634 kdc_log(context
, config
, 0, "AS-REQ %s from %s for %s",
1635 r
->client_name
, from
, r
->server_name
);
1641 if (_kdc_is_anonymous(context
, r
->client_princ
)) {
1642 if (!b
->kdc_options
.request_anonymous
) {
1643 kdc_log(context
, config
, 0, "Anonymous ticket w/o anonymous flag");
1644 ret
= KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN
;
1647 } else if (b
->kdc_options
.request_anonymous
) {
1648 kdc_log(context
, config
, 0,
1649 "Request for a anonymous ticket with non "
1650 "anonymous client name: %s", r
->client_name
);
1651 ret
= KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN
;
1659 ret
= _kdc_db_fetch(context
, config
, r
->client_princ
,
1660 HDB_F_GET_CLIENT
| flags
, NULL
,
1661 &r
->clientdb
, &r
->client
);
1662 if(ret
== HDB_ERR_NOT_FOUND_HERE
) {
1663 kdc_log(context
, config
, 5, "client %s does not have secrets at this KDC, need to proxy",
1667 const char *msg
= krb5_get_error_message(context
, ret
);
1668 kdc_log(context
, config
, 0, "UNKNOWN -- %s: %s", r
->client_name
, msg
);
1669 krb5_free_error_message(context
, msg
);
1670 ret
= KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN
;
1673 ret
= _kdc_db_fetch(context
, config
, r
->server_princ
,
1674 HDB_F_GET_SERVER
|HDB_F_GET_KRBTGT
| flags
,
1675 NULL
, NULL
, &r
->server
);
1676 if(ret
== HDB_ERR_NOT_FOUND_HERE
) {
1677 kdc_log(context
, config
, 5, "target %s does not have secrets at this KDC, need to proxy",
1681 const char *msg
= krb5_get_error_message(context
, ret
);
1682 kdc_log(context
, config
, 0, "UNKNOWN -- %s: %s", r
->server_name
, msg
);
1683 krb5_free_error_message(context
, msg
);
1684 ret
= KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN
;
1689 * Select a session enctype from the list of the crypto system
1690 * supported enctypes that is supported by the client and is one of
1691 * the enctype of the enctype of the service (likely krbtgt).
1693 * The latter is used as a hint of what enctypes all KDC support,
1694 * to make sure a newer version of KDC won't generate a session
1695 * enctype that an older version of a KDC in the same realm can't
1699 ret
= _kdc_find_etype(context
,
1700 krb5_principal_is_krbtgt(context
, r
->server_princ
) ?
1701 config
->tgt_use_strongest_session_key
:
1702 config
->svc_use_strongest_session_key
, FALSE
,
1703 r
->client
, b
->etype
.val
, b
->etype
.len
, &r
->sessionetype
,
1706 kdc_log(context
, config
, 0,
1707 "Client (%s) from %s has no common enctypes with KDC "
1708 "to use for the session key",
1709 r
->client_name
, from
);
1714 * Pre-auth processing
1720 log_patypes(context
, config
, req
->padata
);
1722 /* Check if preauth matching */
1724 for (n
= 0; !found_pa
&& n
< sizeof(pat
) / sizeof(pat
[0]); n
++) {
1725 if (pat
[n
].validate
== NULL
)
1727 if (r
->armor_crypto
== NULL
&& (pat
[n
].flags
& PA_REQ_FAST
))
1730 kdc_log(context
, config
, 5,
1731 "Looking for %s pa-data -- %s", pat
[n
].name
, r
->client_name
);
1733 pa
= _kdc_find_padata(req
, &i
, pat
[n
].type
);
1735 ret
= pat
[n
].validate(r
, pa
);
1737 kdc_log(context
, config
, 0,
1738 "%s pre-authentication succeeded -- %s",
1739 pat
[n
].name
, r
->client_name
);
1741 r
->et
.flags
.pre_authent
= 1;
1747 if (found_pa
== 0 && (require_preauth_p(r
) || b
->kdc_options
.request_anonymous
)) {
1750 for (n
= 0; n
< sizeof(pat
) / sizeof(pat
[0]); n
++) {
1751 if ((pat
[n
].flags
& PA_ANNOUNCE
) == 0)
1753 ret
= krb5_padata_add(context
, &error_method
,
1754 pat
[n
].type
, NULL
, 0);
1760 * If there is a client key, send ETYPE_INFO{,2}
1762 ret
= _kdc_find_etype(context
,
1763 config
->preauth_use_strongest_session_key
, TRUE
,
1764 r
->client
, b
->etype
.val
, b
->etype
.len
, NULL
, &ckey
);
1769 * - If the client only knows about old enctypes, then send
1770 * both info replies (we send 'info' first in the list).
1771 * - If the client is 'modern', because it knows about 'new'
1772 * enctype types, then only send the 'info2' reply.
1774 * Before we send the full list of etype-info data, we pick
1775 * the client key we would have used anyway below, just pick
1779 if (older_enctype(ckey
->key
.keytype
)) {
1780 ret
= get_pa_etype_info(context
, config
,
1781 &error_method
, ckey
);
1785 ret
= get_pa_etype_info2(context
, config
,
1786 &error_method
, ckey
);
1791 ret
= KRB5KDC_ERR_PREAUTH_REQUIRED
;
1792 _kdc_set_e_text(r
, "Need to use PA-ENC-TIMESTAMP/PA-PK-AS-REQ");
1796 if (r
->clientdb
->hdb_auth_status
)
1797 r
->clientdb
->hdb_auth_status(context
, r
->clientdb
, r
->client
,
1801 * Verify flags after the user been required to prove its identity
1802 * with in a preauth mech.
1805 ret
= _kdc_check_access(context
, config
, r
->client
, r
->client_name
,
1806 r
->server
, r
->server_name
,
1807 req
, &error_method
);
1812 * Select the best encryption type for the KDC with out regard to
1813 * the client since the client never needs to read that data.
1816 ret
= _kdc_get_preferred_key(context
, config
,
1817 r
->server
, r
->server_name
,
1822 if(f
.renew
|| f
.validate
|| f
.proxy
|| f
.forwarded
|| f
.enc_tkt_in_skey
1823 || (f
.request_anonymous
&& !config
->allow_anonymous
)) {
1824 ret
= KRB5KDC_ERR_BADOPTION
;
1825 _kdc_set_e_text(r
, "Bad KDC options");
1834 rep
.msg_type
= krb_as_rep
;
1836 ret
= copy_Realm(&r
->client
->entry
.principal
->realm
, &rep
.crealm
);
1839 ret
= _krb5_principal2principalname(&rep
.cname
, r
->client
->entry
.principal
);
1843 rep
.ticket
.tkt_vno
= 5;
1844 copy_Realm(&r
->server
->entry
.principal
->realm
, &rep
.ticket
.realm
);
1845 _krb5_principal2principalname(&rep
.ticket
.sname
,
1846 r
->server
->entry
.principal
);
1847 /* java 1.6 expects the name to be the same type, lets allow that
1848 * uncomplicated name-types. */
1849 #define CNT(sp,t) (((sp)->sname->name_type) == KRB5_NT_##t)
1850 if (CNT(b
, UNKNOWN
) || CNT(b
, PRINCIPAL
) || CNT(b
, SRV_INST
) || CNT(b
, SRV_HST
) || CNT(b
, SRV_XHST
))
1851 rep
.ticket
.sname
.name_type
= b
->sname
->name_type
;
1854 r
->et
.flags
.initial
= 1;
1855 if(r
->client
->entry
.flags
.forwardable
&& r
->server
->entry
.flags
.forwardable
)
1856 r
->et
.flags
.forwardable
= f
.forwardable
;
1857 else if (f
.forwardable
) {
1858 _kdc_set_e_text(r
, "Ticket may not be forwardable");
1859 ret
= KRB5KDC_ERR_POLICY
;
1862 if(r
->client
->entry
.flags
.proxiable
&& r
->server
->entry
.flags
.proxiable
)
1863 r
->et
.flags
.proxiable
= f
.proxiable
;
1864 else if (f
.proxiable
) {
1865 _kdc_set_e_text(r
, "Ticket may not be proxiable");
1866 ret
= KRB5KDC_ERR_POLICY
;
1869 if(r
->client
->entry
.flags
.postdate
&& r
->server
->entry
.flags
.postdate
)
1870 r
->et
.flags
.may_postdate
= f
.allow_postdate
;
1871 else if (f
.allow_postdate
){
1872 _kdc_set_e_text(r
, "Ticket may not be postdate");
1873 ret
= KRB5KDC_ERR_POLICY
;
1877 /* check for valid set of addresses */
1878 if(!_kdc_check_addresses(context
, config
, b
->addresses
, from_addr
)) {
1879 _kdc_set_e_text(r
, "Bad address list in requested");
1880 ret
= KRB5KRB_AP_ERR_BADADDR
;
1884 ret
= copy_PrincipalName(&rep
.cname
, &r
->et
.cname
);
1887 ret
= copy_Realm(&rep
.crealm
, &r
->et
.crealm
);
1895 start
= r
->et
.authtime
= kdc_time
;
1897 if(f
.postdated
&& req
->req_body
.from
){
1898 ALLOC(r
->et
.starttime
);
1899 start
= *r
->et
.starttime
= *req
->req_body
.from
;
1900 r
->et
.flags
.invalid
= 1;
1901 r
->et
.flags
.postdated
= 1; /* XXX ??? */
1903 _kdc_fix_time(&b
->till
);
1906 /* be careful not overflowing */
1908 if(r
->client
->entry
.max_life
)
1909 t
= start
+ min(t
- start
, *r
->client
->entry
.max_life
);
1910 if(r
->server
->entry
.max_life
)
1911 t
= start
+ min(t
- start
, *r
->server
->entry
.max_life
);
1913 t
= min(t
, start
+ realm
->max_life
);
1916 if(f
.renewable_ok
&& r
->et
.endtime
< *b
->till
){
1918 if(b
->rtime
== NULL
){
1922 if(*b
->rtime
< *b
->till
)
1923 *b
->rtime
= *b
->till
;
1925 if(f
.renewable
&& b
->rtime
){
1929 if(r
->client
->entry
.max_renew
)
1930 t
= start
+ min(t
- start
, *r
->client
->entry
.max_renew
);
1931 if(r
->server
->entry
.max_renew
)
1932 t
= start
+ min(t
- start
, *r
->server
->entry
.max_renew
);
1934 t
= min(t
, start
+ realm
->max_renew
);
1936 ALLOC(r
->et
.renew_till
);
1937 *r
->et
.renew_till
= t
;
1938 r
->et
.flags
.renewable
= 1;
1942 if (f
.request_anonymous
)
1943 r
->et
.flags
.anonymous
= 1;
1947 copy_HostAddresses(b
->addresses
, r
->et
.caddr
);
1950 r
->et
.transited
.tr_type
= DOMAIN_X500_COMPRESS
;
1951 krb5_data_zero(&r
->et
.transited
.contents
);
1953 /* The MIT ASN.1 library (obviously) doesn't tell lengths encoded
1954 * as 0 and as 0x80 (meaning indefinite length) apart, and is thus
1955 * incapable of correctly decoding SEQUENCE OF's of zero length.
1957 * To fix this, always send at least one no-op last_req
1959 * If there's a pw_end or valid_end we will use that,
1960 * otherwise just a dummy lr.
1962 r
->ek
.last_req
.val
= malloc(2 * sizeof(*r
->ek
.last_req
.val
));
1963 if (r
->ek
.last_req
.val
== NULL
) {
1967 r
->ek
.last_req
.len
= 0;
1968 if (r
->client
->entry
.pw_end
1969 && (config
->kdc_warn_pwexpire
== 0
1970 || kdc_time
+ config
->kdc_warn_pwexpire
>= *r
->client
->entry
.pw_end
)) {
1971 r
->ek
.last_req
.val
[r
->ek
.last_req
.len
].lr_type
= LR_PW_EXPTIME
;
1972 r
->ek
.last_req
.val
[r
->ek
.last_req
.len
].lr_value
= *r
->client
->entry
.pw_end
;
1973 ++r
->ek
.last_req
.len
;
1975 if (r
->client
->entry
.valid_end
) {
1976 r
->ek
.last_req
.val
[r
->ek
.last_req
.len
].lr_type
= LR_ACCT_EXPTIME
;
1977 r
->ek
.last_req
.val
[r
->ek
.last_req
.len
].lr_value
= *r
->client
->entry
.valid_end
;
1978 ++r
->ek
.last_req
.len
;
1980 if (r
->ek
.last_req
.len
== 0) {
1981 r
->ek
.last_req
.val
[r
->ek
.last_req
.len
].lr_type
= LR_NONE
;
1982 r
->ek
.last_req
.val
[r
->ek
.last_req
.len
].lr_value
= 0;
1983 ++r
->ek
.last_req
.len
;
1985 r
->ek
.nonce
= b
->nonce
;
1986 if (r
->client
->entry
.valid_end
|| r
->client
->entry
.pw_end
) {
1987 ALLOC(r
->ek
.key_expiration
);
1988 if (r
->client
->entry
.valid_end
) {
1989 if (r
->client
->entry
.pw_end
)
1990 *r
->ek
.key_expiration
= min(*r
->client
->entry
.valid_end
,
1991 *r
->client
->entry
.pw_end
);
1993 *r
->ek
.key_expiration
= *r
->client
->entry
.valid_end
;
1995 *r
->ek
.key_expiration
= *r
->client
->entry
.pw_end
;
1997 r
->ek
.key_expiration
= NULL
;
1998 r
->ek
.flags
= r
->et
.flags
;
1999 r
->ek
.authtime
= r
->et
.authtime
;
2000 if (r
->et
.starttime
) {
2001 ALLOC(r
->ek
.starttime
);
2002 *r
->ek
.starttime
= *r
->et
.starttime
;
2004 r
->ek
.endtime
= r
->et
.endtime
;
2005 if (r
->et
.renew_till
) {
2006 ALLOC(r
->ek
.renew_till
);
2007 *r
->ek
.renew_till
= *r
->et
.renew_till
;
2009 copy_Realm(&rep
.ticket
.realm
, &r
->ek
.srealm
);
2010 copy_PrincipalName(&rep
.ticket
.sname
, &r
->ek
.sname
);
2013 copy_HostAddresses(r
->et
.caddr
, r
->ek
.caddr
);
2017 * Check and session and reply keys
2020 if (r
->session_key
.keytype
== ETYPE_NULL
) {
2021 ret
= krb5_generate_random_keyblock(context
, r
->sessionetype
, &r
->session_key
);
2026 if (r
->reply_key
.keytype
== ETYPE_NULL
) {
2027 _kdc_set_e_text(r
, "Client have no reply key");
2028 ret
= KRB5KDC_ERR_CLIENT_NOTYET
;
2032 ret
= copy_EncryptionKey(&r
->session_key
, &r
->et
.key
);
2036 ret
= copy_EncryptionKey(&r
->session_key
, &r
->ek
.key
);
2041 * Add signing of alias referral
2044 if (f
.canonicalize
) {
2045 PA_ClientCanonicalized canon
;
2048 krb5_crypto cryptox
;
2051 memset(&canon
, 0, sizeof(canon
));
2053 canon
.names
.requested_name
= *b
->cname
;
2054 canon
.names
.mapped_name
= r
->client
->entry
.principal
->name
;
2056 ASN1_MALLOC_ENCODE(PA_ClientCanonicalizedNames
, data
.data
, data
.length
,
2057 &canon
.names
, &len
, ret
);
2060 if (data
.length
!= len
)
2061 krb5_abortx(context
, "internal asn.1 error");
2063 /* sign using "returned session key" */
2064 ret
= krb5_crypto_init(context
, &r
->et
.key
, 0, &cryptox
);
2070 ret
= krb5_create_checksum(context
, cryptox
,
2071 KRB5_KU_CANONICALIZED_NAMES
, 0,
2072 data
.data
, data
.length
,
2073 &canon
.canon_checksum
);
2075 krb5_crypto_destroy(context
, cryptox
);
2079 ASN1_MALLOC_ENCODE(PA_ClientCanonicalized
, data
.data
, data
.length
,
2081 free_Checksum(&canon
.canon_checksum
);
2084 if (data
.length
!= len
)
2085 krb5_abortx(context
, "internal asn.1 error");
2087 tmppa
.padata_type
= KRB5_PADATA_CLIENT_CANONICALIZED
;
2088 tmppa
.padata_value
= data
;
2089 ret
= add_METHOD_DATA(&r
->outpadata
, &tmppa
);
2095 if (r
->outpadata
.len
) {
2098 if (rep
.padata
== NULL
) {
2102 ret
= copy_METHOD_DATA(&r
->outpadata
, rep
.padata
);
2108 if (send_pac_p(context
, req
)) {
2109 generate_pac(r
, skey
);
2112 _kdc_log_timestamp(context
, config
, "AS-REQ", r
->et
.authtime
, r
->et
.starttime
,
2113 r
->et
.endtime
, r
->et
.renew_till
);
2115 /* do this as the last thing since this signs the EncTicketPart */
2116 ret
= _kdc_add_KRB5SignedPath(context
,
2120 r
->client
->entry
.principal
,
2127 log_as_req(context
, config
, r
->reply_key
.keytype
, setype
, b
);
2130 * Add REQ_ENC_PA_REP if client supports it
2134 pa
= _kdc_find_padata(req
, &i
, KRB5_PADATA_REQ_ENC_PA_REP
);
2137 ret
= add_enc_pa_rep(r
);
2139 const char *msg
= krb5_get_error_message(r
->context
, ret
);
2140 _kdc_r_log(r
, 0, "add_enc_pa_rep failed: %s: %d", msg
, ret
);
2141 krb5_free_error_message(r
->context
, msg
);
2150 ret
= _kdc_encode_reply(context
, config
,
2151 r
->armor_crypto
, req
->req_body
.nonce
,
2152 &rep
, &r
->et
, &r
->ek
, setype
, r
->server
->entry
.kvno
,
2153 &skey
->key
, r
->client
->entry
.kvno
,
2154 &r
->reply_key
, 0, &r
->e_text
, reply
);
2159 * Check if message too large
2161 if (datagram_reply
&& reply
->length
> config
->max_datagram_reply_length
) {
2162 krb5_data_free(reply
);
2163 ret
= KRB5KRB_ERR_RESPONSE_TOO_BIG
;
2164 _kdc_set_e_text(r
, "Reply packet too large");
2171 * In case of a non proxy error, build an error message.
2173 if(ret
!= 0 && ret
!= HDB_ERR_NOT_FOUND_HERE
) {
2174 ret
= _kdc_fast_mk_error(context
, r
,
2179 r
->client_princ
, r
->server_princ
,
2186 free_EncTicketPart(&r
->et
);
2187 free_EncKDCRepPart(&r
->ek
);
2188 free_KDCFastState(&r
->fast
);
2190 if (error_method
.len
)
2191 free_METHOD_DATA(&error_method
);
2192 if (r
->outpadata
.len
)
2193 free_METHOD_DATA(&r
->outpadata
);
2194 if (r
->client_princ
) {
2195 krb5_free_principal(context
, r
->client_princ
);
2196 r
->client_princ
= NULL
;
2198 if (r
->client_name
) {
2199 free(r
->client_name
);
2200 r
->client_name
= NULL
;
2202 if (r
->server_princ
){
2203 krb5_free_principal(context
, r
->server_princ
);
2204 r
->server_princ
= NULL
;
2206 if (r
->server_name
) {
2207 free(r
->server_name
);
2208 r
->server_name
= NULL
;
2211 _kdc_free_ent(context
, r
->client
);
2213 _kdc_free_ent(context
, r
->server
);
2214 if (r
->armor_crypto
) {
2215 krb5_crypto_destroy(r
->context
, r
->armor_crypto
);
2216 r
->armor_crypto
= NULL
;
2218 krb5_free_keyblock_contents(r
->context
, &r
->reply_key
);
2219 krb5_free_keyblock_contents(r
->context
, &r
->session_key
);
2224 * Add the AuthorizationData `data´ of `type´ to the last element in
2225 * the sequence of authorization_data in `tkt´ wrapped in an IF_RELEVANT
2229 _kdc_tkt_add_if_relevant_ad(krb5_context context
,
2232 const krb5_data
*data
)
2234 krb5_error_code ret
;
2237 if (tkt
->authorization_data
== NULL
) {
2238 tkt
->authorization_data
= calloc(1, sizeof(*tkt
->authorization_data
));
2239 if (tkt
->authorization_data
== NULL
) {
2240 krb5_set_error_message(context
, ENOMEM
, "out of memory");
2245 /* add the entry to the last element */
2247 AuthorizationData ad
= { 0, NULL
};
2248 AuthorizationDataElement ade
;
2251 ade
.ad_data
= *data
;
2253 ret
= add_AuthorizationData(&ad
, &ade
);
2255 krb5_set_error_message(context
, ret
, "add AuthorizationData failed");
2259 ade
.ad_type
= KRB5_AUTHDATA_IF_RELEVANT
;
2261 ASN1_MALLOC_ENCODE(AuthorizationData
,
2262 ade
.ad_data
.data
, ade
.ad_data
.length
,
2264 free_AuthorizationData(&ad
);
2266 krb5_set_error_message(context
, ret
, "ASN.1 encode of "
2267 "AuthorizationData failed");
2270 if (ade
.ad_data
.length
!= size
)
2271 krb5_abortx(context
, "internal asn.1 encoder error");
2273 ret
= add_AuthorizationData(tkt
->authorization_data
, &ade
);
2274 der_free_octet_string(&ade
.ad_data
);
2276 krb5_set_error_message(context
, ret
, "add AuthorizationData failed");