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 int invalidPassword
= 0;
427 EncryptedData enc_data
;
428 krb5_enctype aenctype
;
434 heim_assert(r
->armor_crypto
!= NULL
, "ENC-CHAL called for non FAST");
436 if (_kdc_is_anon_request(b
)) {
437 ret
= KRB5KRB_AP_ERR_BAD_INTEGRITY
;
438 kdc_log(r
->context
, r
->config
, 0, "ENC-CHALL doesn't support anon");
442 ret
= decode_EncryptedData(pa
->padata_value
.data
,
443 pa
->padata_value
.length
,
447 ret
= KRB5KRB_AP_ERR_BAD_INTEGRITY
;
448 _kdc_r_log(r
, 5, "Failed to decode PA-DATA -- %s",
453 pepper1
.data
= "clientchallengearmor";
454 pepper1
.length
= strlen(pepper1
.data
);
455 pepper2
.data
= "challengelongterm";
456 pepper2
.length
= strlen(pepper2
.data
);
458 krb5_crypto_getenctype(r
->context
, r
->armor_crypto
, &aenctype
);
460 for (i
= 0; i
< r
->client
->entry
.keys
.len
; i
++) {
461 krb5_crypto challangecrypto
, longtermcrypto
;
462 krb5_keyblock challangekey
;
465 k
= &r
->client
->entry
.keys
.val
[i
];
467 ret
= krb5_crypto_init(r
->context
, &k
->key
, 0, &longtermcrypto
);
471 ret
= krb5_crypto_fx_cf2(r
->context
, r
->armor_crypto
, longtermcrypto
,
472 &pepper1
, &pepper2
, aenctype
,
474 krb5_crypto_destroy(r
->context
, longtermcrypto
);
478 ret
= krb5_crypto_init(r
->context
, &challangekey
, 0,
483 ret
= krb5_decrypt_EncryptedData(r
->context
, challangecrypto
,
484 KRB5_KU_ENC_CHALLENGE_CLIENT
,
488 const char *msg
= krb5_get_error_message(r
->context
, ret
);
489 krb5_error_code ret2
;
494 ret2
= krb5_enctype_to_string(r
->context
, k
->key
.keytype
, &str
);
497 _kdc_r_log(r
, 5, "Failed to decrypt ENC-CHAL -- %s "
498 "(enctype %s) error %s",
499 r
->client_name
, str
? str
: "unknown enctype", msg
);
500 krb5_free_error_message(r
->context
, msg
);
506 ret
= decode_PA_ENC_TS_ENC(ts_data
.data
,
510 krb5_data_free(&ts_data
);
512 krb5_crypto_destroy(r
->context
, challangecrypto
);
513 ret
= KRB5KDC_ERR_PREAUTH_FAILED
;
514 _kdc_r_log(r
, 5, "Failed to decode PA-ENC-TS_ENC -- %s",
519 if (labs(kdc_time
- p
.patimestamp
) > r
->context
->max_skew
) {
520 char client_time
[100];
522 krb5_crypto_destroy(r
->context
, challangecrypto
);
524 krb5_format_time(r
->context
, p
.patimestamp
,
525 client_time
, sizeof(client_time
), TRUE
);
527 ret
= KRB5KRB_AP_ERR_SKEW
;
528 _kdc_r_log(r
, 0, "Too large time skew, "
529 "client time %s is out by %u > %u seconds -- %s",
531 (unsigned)labs(kdc_time
- p
.patimestamp
),
532 r
->context
->max_skew
,
535 free_PA_ENC_TS_ENC(&p
);
539 free_PA_ENC_TS_ENC(&p
);
541 ret
= make_pa_enc_challange(r
->context
, &r
->outpadata
,
543 krb5_crypto_destroy(r
->context
, challangecrypto
);
547 set_salt_padata(&r
->outpadata
, k
->salt
);
548 krb5_free_keyblock_contents(r
->context
, &r
->reply_key
);
549 ret
= krb5_copy_keyblock_contents(r
->context
, &k
->key
, &r
->reply_key
);
556 if (r
->clientdb
->hdb_auth_status
)
557 r
->clientdb
->hdb_auth_status(r
->context
, r
->clientdb
, r
->client
,
562 if (invalidPassword
&& r
->clientdb
->hdb_auth_status
) {
563 r
->clientdb
->hdb_auth_status(r
->context
, r
->clientdb
, r
->client
,
564 HDB_AUTH_WRONG_PASSWORD
);
565 ret
= KRB5KDC_ERR_PREAUTH_FAILED
;
568 free_EncryptedData(&enc_data
);
573 static krb5_error_code
574 pa_enc_ts_validate(kdc_request_t r
, const PA_DATA
*pa
)
576 EncryptedData enc_data
;
585 if (_kdc_is_anon_request(&r
->req
.req_body
)) {
586 ret
= KRB5KRB_AP_ERR_BAD_INTEGRITY
;
587 _kdc_set_e_text(r
, "ENC-TS doesn't support anon");
591 ret
= decode_EncryptedData(pa
->padata_value
.data
,
592 pa
->padata_value
.length
,
596 ret
= KRB5KRB_AP_ERR_BAD_INTEGRITY
;
597 _kdc_r_log(r
, 5, "Failed to decode PA-DATA -- %s",
602 ret
= hdb_enctype2key(r
->context
, &r
->client
->entry
, NULL
,
603 enc_data
.etype
, &pa_key
);
606 _kdc_set_e_text(r
, "No key matching entype");
607 ret
= KRB5KDC_ERR_ETYPE_NOSUPP
;
608 if(krb5_enctype_to_string(r
->context
, enc_data
.etype
, &estr
))
612 "No client key matching pa-data (%d) -- %s",
613 enc_data
.etype
, r
->client_name
);
616 "No client key matching pa-data (%s) -- %s",
617 estr
, r
->client_name
);
619 free_EncryptedData(&enc_data
);
624 ret
= krb5_crypto_init(r
->context
, &pa_key
->key
, 0, &crypto
);
626 const char *msg
= krb5_get_error_message(r
->context
, ret
);
627 _kdc_r_log(r
, 0, "krb5_crypto_init failed: %s", msg
);
628 krb5_free_error_message(r
->context
, msg
);
629 free_EncryptedData(&enc_data
);
633 ret
= krb5_decrypt_EncryptedData (r
->context
,
635 KRB5_KU_PA_ENC_TIMESTAMP
,
638 krb5_crypto_destroy(r
->context
, crypto
);
640 * Since the user might have several keys with the same
641 * enctype but with diffrent salting, we need to try all
642 * the keys with the same enctype.
645 krb5_error_code ret2
;
646 const char *msg
= krb5_get_error_message(r
->context
, ret
);
648 ret2
= krb5_enctype_to_string(r
->context
,
649 pa_key
->key
.keytype
, &str
);
652 _kdc_r_log(r
, 5, "Failed to decrypt PA-DATA -- %s "
653 "(enctype %s) error %s",
654 r
->client_name
, str
? str
: "unknown enctype", msg
);
655 krb5_free_error_message(r
->context
, msg
);
658 if(hdb_next_enctype2key(r
->context
, &r
->client
->entry
, NULL
,
659 enc_data
.etype
, &pa_key
) == 0)
662 free_EncryptedData(&enc_data
);
664 if (r
->clientdb
->hdb_auth_status
)
665 r
->clientdb
->hdb_auth_status(r
->context
, r
->clientdb
, r
->client
,
666 HDB_AUTH_WRONG_PASSWORD
);
668 ret
= KRB5KDC_ERR_PREAUTH_FAILED
;
671 free_EncryptedData(&enc_data
);
672 ret
= decode_PA_ENC_TS_ENC(ts_data
.data
,
676 krb5_data_free(&ts_data
);
678 ret
= KRB5KDC_ERR_PREAUTH_FAILED
;
679 _kdc_r_log(r
, 5, "Failed to decode PA-ENC-TS_ENC -- %s",
683 if (labs(kdc_time
- p
.patimestamp
) > r
->context
->max_skew
) {
684 char client_time
[100];
686 krb5_format_time(r
->context
, p
.patimestamp
,
687 client_time
, sizeof(client_time
), TRUE
);
689 ret
= KRB5KRB_AP_ERR_SKEW
;
690 _kdc_r_log(r
, 0, "Too large time skew, "
691 "client time %s is out by %u > %u seconds -- %s",
693 (unsigned)labs(kdc_time
- p
.patimestamp
),
694 r
->context
->max_skew
,
698 * The following is needed to make windows clients to
699 * retry using the timestamp in the error message, if
700 * there is a e_text, they become unhappy.
703 free_PA_ENC_TS_ENC(&p
);
706 free_PA_ENC_TS_ENC(&p
);
708 set_salt_padata(&r
->outpadata
, pa_key
->salt
);
710 ret
= krb5_copy_keyblock_contents(r
->context
, &pa_key
->key
, &r
->reply_key
);
714 ret
= krb5_enctype_to_string(r
->context
, pa_key
->key
.keytype
, &str
);
717 _kdc_r_log(r
, 2, "ENC-TS Pre-authentication succeeded -- %s using %s",
718 r
->client_name
, str
? str
: "unknown enctype");
732 #define PA_ANNOUNCE 1
733 #define PA_REQ_FAST 2 /* only use inside fast */
734 krb5_error_code (*validate
)(kdc_request_t
, const PA_DATA
*pa
);
737 static const struct kdc_patypes pat
[] = {
740 KRB5_PADATA_PK_AS_REQ
, "PK-INIT(ietf)", PA_ANNOUNCE
,
744 KRB5_PADATA_PK_AS_REQ_WIN
, "PK-INIT(win2k)", PA_ANNOUNCE
,
748 KRB5_PADATA_PKINIT_KX
, "Anonymous PK-INIT", PA_ANNOUNCE
,
752 { KRB5_PADATA_PK_AS_REQ
, "PK-INIT(ietf)", 0, NULL
},
753 { KRB5_PADATA_PK_AS_REQ_WIN
, "PK-INIT(win2k)", 0, NULL
},
754 { KRB5_PADATA_PKINIT_KX
, "Anonymous PK-INIT", 0, NULL
},
756 { KRB5_PADATA_PA_PK_OCSP_RESPONSE
, "OCSP", 0, NULL
},
758 KRB5_PADATA_ENC_TIMESTAMP
, "ENC-TS",
763 KRB5_PADATA_ENCRYPTED_CHALLENGE
, "ENC-CHAL",
764 PA_ANNOUNCE
| PA_REQ_FAST
,
767 { KRB5_PADATA_REQ_ENC_PA_REP
, "REQ-ENC-PA-REP", 0, NULL
},
768 { KRB5_PADATA_FX_FAST
, "FX-FAST", PA_ANNOUNCE
, NULL
},
769 { KRB5_PADATA_FX_ERROR
, "FX-ERROR", 0, NULL
},
770 { KRB5_PADATA_FX_COOKIE
, "FX-COOKIE", 0, NULL
}
774 log_patypes(krb5_context context
,
775 krb5_kdc_configuration
*config
,
778 struct rk_strpool
*p
= NULL
;
782 for (n
= 0; n
< padata
->len
; n
++) {
783 for (m
= 0; m
< sizeof(pat
) / sizeof(pat
[0]); m
++) {
784 if (padata
->val
[n
].padata_type
== pat
[m
].type
) {
785 p
= rk_strpoolprintf(p
, "%s", pat
[m
].name
);
789 if (m
== sizeof(pat
) / sizeof(pat
[0]))
790 p
= rk_strpoolprintf(p
, "%d", padata
->val
[n
].padata_type
);
791 if (p
&& n
+ 1 < padata
->len
)
792 p
= rk_strpoolprintf(p
, ", ");
794 kdc_log(context
, config
, 0, "out of memory");
799 p
= rk_strpoolprintf(p
, "none");
801 str
= rk_strpoolcollect(p
);
802 kdc_log(context
, config
, 0, "Client sent patypes: %s", str
);
811 _kdc_encode_reply(krb5_context context
,
812 krb5_kdc_configuration
*config
,
813 krb5_crypto armor_crypto
, uint32_t nonce
,
814 KDC_REP
*rep
, EncTicketPart
*et
, EncKDCRepPart
*ek
,
816 int skvno
, const EncryptionKey
*skey
,
817 int ckvno
, const EncryptionKey
*reply_key
,
828 ASN1_MALLOC_ENCODE(EncTicketPart
, buf
, buf_size
, et
, &len
, ret
);
830 const char *msg
= krb5_get_error_message(context
, ret
);
831 kdc_log(context
, config
, 0, "Failed to encode ticket: %s", msg
);
832 krb5_free_error_message(context
, msg
);
836 krb5_abortx(context
, "Internal error in ASN.1 encoder");
838 ret
= krb5_crypto_init(context
, skey
, etype
, &crypto
);
840 const char *msg
= krb5_get_error_message(context
, ret
);
841 kdc_log(context
, config
, 0, "krb5_crypto_init failed: %s", msg
);
842 krb5_free_error_message(context
, msg
);
846 ret
= krb5_encrypt_EncryptedData(context
,
852 &rep
->ticket
.enc_part
);
854 krb5_crypto_destroy(context
, crypto
);
856 const char *msg
= krb5_get_error_message(context
, ret
);
857 kdc_log(context
, config
, 0, "Failed to encrypt data: %s", msg
);
858 krb5_free_error_message(context
, msg
);
864 krb5_keyblock
*strengthen_key
= NULL
;
865 KrbFastFinished finished
;
867 kdc_log(context
, config
, 0, "FAST armor protection");
869 memset(&finished
, 0, sizeof(finished
));
870 krb5_data_zero(&data
);
872 finished
.timestamp
= kdc_time
;
874 finished
.crealm
= et
->crealm
;
875 finished
.cname
= et
->cname
;
877 ASN1_MALLOC_ENCODE(Ticket
, data
.data
, data
.length
,
878 &rep
->ticket
, &len
, ret
);
881 if (data
.length
!= len
)
882 krb5_abortx(context
, "internal asn.1 error");
884 ret
= krb5_create_checksum(context
, armor_crypto
,
885 KRB5_KU_FAST_FINISHED
, 0,
886 data
.data
, data
.length
,
887 &finished
.ticket_checksum
);
888 krb5_data_free(&data
);
892 ret
= _kdc_fast_mk_response(context
, armor_crypto
,
893 rep
->padata
, strengthen_key
, &finished
,
895 free_Checksum(&finished
.ticket_checksum
);
900 free_METHOD_DATA(rep
->padata
);
902 rep
->padata
= calloc(1, sizeof(*(rep
->padata
)));
903 if (rep
->padata
== NULL
) {
904 krb5_data_free(&data
);
909 ret
= krb5_padata_add(context
, rep
->padata
,
911 data
.data
, data
.length
);
916 * Hide client name of privacy reasons
918 if (1 /* r->fast_options.hide_client_names */) {
919 rep
->crealm
[0] = '\0';
920 free_PrincipalName(&rep
->cname
);
921 rep
->cname
.name_type
= 0;
925 if(rep
->msg_type
== krb_as_rep
&& !config
->encode_as_rep_as_tgs_rep
)
926 ASN1_MALLOC_ENCODE(EncASRepPart
, buf
, buf_size
, ek
, &len
, ret
);
928 ASN1_MALLOC_ENCODE(EncTGSRepPart
, buf
, buf_size
, ek
, &len
, ret
);
930 const char *msg
= krb5_get_error_message(context
, ret
);
931 kdc_log(context
, config
, 0, "Failed to encode KDC-REP: %s", msg
);
932 krb5_free_error_message(context
, msg
);
935 if(buf_size
!= len
) {
937 kdc_log(context
, config
, 0, "Internal error in ASN.1 encoder");
938 *e_text
= "KDC internal error";
939 return KRB5KRB_ERR_GENERIC
;
941 ret
= krb5_crypto_init(context
, reply_key
, 0, &crypto
);
943 const char *msg
= krb5_get_error_message(context
, ret
);
945 kdc_log(context
, config
, 0, "krb5_crypto_init failed: %s", msg
);
946 krb5_free_error_message(context
, msg
);
949 if(rep
->msg_type
== krb_as_rep
) {
950 krb5_encrypt_EncryptedData(context
,
952 KRB5_KU_AS_REP_ENC_PART
,
958 ASN1_MALLOC_ENCODE(AS_REP
, buf
, buf_size
, rep
, &len
, ret
);
960 krb5_encrypt_EncryptedData(context
,
962 rk_is_subkey
? KRB5_KU_TGS_REP_ENC_PART_SUB_KEY
: KRB5_KU_TGS_REP_ENC_PART_SESSION
,
968 ASN1_MALLOC_ENCODE(TGS_REP
, buf
, buf_size
, rep
, &len
, ret
);
970 krb5_crypto_destroy(context
, crypto
);
972 const char *msg
= krb5_get_error_message(context
, ret
);
973 kdc_log(context
, config
, 0, "Failed to encode KDC-REP: %s", msg
);
974 krb5_free_error_message(context
, msg
);
977 if(buf_size
!= len
) {
979 kdc_log(context
, config
, 0, "Internal error in ASN.1 encoder");
980 *e_text
= "KDC internal error";
981 return KRB5KRB_ERR_GENERIC
;
984 reply
->length
= buf_size
;
989 * Return 1 if the client have only older enctypes, this is for
990 * determining if the server should send ETYPE_INFO2 or not.
994 older_enctype(krb5_enctype enctype
)
997 case ETYPE_DES_CBC_CRC
:
998 case ETYPE_DES_CBC_MD4
:
999 case ETYPE_DES_CBC_MD5
:
1000 case ETYPE_DES3_CBC_SHA1
:
1001 case ETYPE_ARCFOUR_HMAC_MD5
:
1002 case ETYPE_ARCFOUR_HMAC_MD5_56
:
1004 * The following three is "old" windows enctypes and is needed for
1005 * windows 2000 hosts.
1007 case ETYPE_ARCFOUR_MD4
:
1008 case ETYPE_ARCFOUR_HMAC_OLD
:
1009 case ETYPE_ARCFOUR_HMAC_OLD_EXP
:
1020 static krb5_error_code
1021 make_etype_info_entry(krb5_context context
, ETYPE_INFO_ENTRY
*ent
, Key
*key
)
1023 ent
->etype
= key
->key
.keytype
;
1026 ALLOC(ent
->salttype
);
1028 if(key
->salt
->type
== hdb_pw_salt
)
1029 *ent
->salttype
= 0; /* or 1? or NULL? */
1030 else if(key
->salt
->type
== hdb_afs3_salt
)
1033 kdc_log(context
, config
, 0, "unknown salt-type: %d",
1035 return KRB5KRB_ERR_GENERIC
;
1037 /* according to `the specs', we can't send a salt if
1038 we have AFS3 salted key, but that requires that you
1039 *know* what cell you are using (e.g by assuming
1040 that the cell is the same as the realm in lower
1043 ALLOC(ent
->salttype
);
1044 *ent
->salttype
= key
->salt
->type
;
1047 * We shouldn't sent salttype since it is incompatible with the
1048 * specification and it breaks windows clients. The afs
1049 * salting problem is solved by using KRB5-PADATA-AFS3-SALT
1050 * implemented in Heimdal 0.7 and later.
1052 ent
->salttype
= NULL
;
1054 krb5_copy_data(context
, &key
->salt
->salt
,
1057 /* we return no salt type at all, as that should indicate
1058 * the default salt type and make everybody happy. some
1059 * systems (like w2k) dislike being told the salt type
1062 ent
->salttype
= NULL
;
1068 static krb5_error_code
1069 get_pa_etype_info(krb5_context context
,
1070 krb5_kdc_configuration
*config
,
1071 METHOD_DATA
*md
, Key
*ckey
)
1073 krb5_error_code ret
= 0;
1080 pa
.val
= calloc(1, sizeof(pa
.val
[0]));
1084 ret
= make_etype_info_entry(context
, &pa
.val
[0], ckey
);
1086 free_ETYPE_INFO(&pa
);
1090 ASN1_MALLOC_ENCODE(ETYPE_INFO
, buf
, len
, &pa
, &len
, ret
);
1091 free_ETYPE_INFO(&pa
);
1094 ret
= realloc_method_data(md
);
1099 md
->val
[md
->len
- 1].padata_type
= KRB5_PADATA_ETYPE_INFO
;
1100 md
->val
[md
->len
- 1].padata_value
.length
= len
;
1101 md
->val
[md
->len
- 1].padata_value
.data
= buf
;
1109 extern int _krb5_AES_string_to_default_iterator
;
1111 static krb5_error_code
1112 make_etype_info2_entry(ETYPE_INFO2_ENTRY
*ent
, Key
*key
)
1114 ent
->etype
= key
->key
.keytype
;
1117 if (ent
->salt
== NULL
)
1119 *ent
->salt
= malloc(key
->salt
->salt
.length
+ 1);
1120 if (*ent
->salt
== NULL
) {
1125 memcpy(*ent
->salt
, key
->salt
->salt
.data
, key
->salt
->salt
.length
);
1126 (*ent
->salt
)[key
->salt
->salt
.length
] = '\0';
1130 ent
->s2kparams
= NULL
;
1132 switch (key
->key
.keytype
) {
1133 case ETYPE_AES128_CTS_HMAC_SHA1_96
:
1134 case ETYPE_AES256_CTS_HMAC_SHA1_96
:
1135 ALLOC(ent
->s2kparams
);
1136 if (ent
->s2kparams
== NULL
)
1138 ent
->s2kparams
->length
= 4;
1139 ent
->s2kparams
->data
= malloc(ent
->s2kparams
->length
);
1140 if (ent
->s2kparams
->data
== NULL
) {
1141 free(ent
->s2kparams
);
1142 ent
->s2kparams
= NULL
;
1145 _krb5_put_int(ent
->s2kparams
->data
,
1146 _krb5_AES_string_to_default_iterator
,
1147 ent
->s2kparams
->length
);
1149 case ETYPE_DES_CBC_CRC
:
1150 case ETYPE_DES_CBC_MD4
:
1151 case ETYPE_DES_CBC_MD5
:
1152 /* Check if this was a AFS3 salted key */
1153 if(key
->salt
&& key
->salt
->type
== hdb_afs3_salt
){
1154 ALLOC(ent
->s2kparams
);
1155 if (ent
->s2kparams
== NULL
)
1157 ent
->s2kparams
->length
= 1;
1158 ent
->s2kparams
->data
= malloc(ent
->s2kparams
->length
);
1159 if (ent
->s2kparams
->data
== NULL
) {
1160 free(ent
->s2kparams
);
1161 ent
->s2kparams
= NULL
;
1164 _krb5_put_int(ent
->s2kparams
->data
,
1166 ent
->s2kparams
->length
);
1176 * Return an ETYPE-INFO2. Enctypes are storted the same way as in the
1177 * database (client supported enctypes first, then the unsupported
1181 static krb5_error_code
1182 get_pa_etype_info2(krb5_context context
,
1183 krb5_kdc_configuration
*config
,
1184 METHOD_DATA
*md
, Key
*ckey
)
1186 krb5_error_code ret
= 0;
1192 pa
.val
= calloc(1, sizeof(pa
.val
[0]));
1196 ret
= make_etype_info2_entry(&pa
.val
[0], ckey
);
1198 free_ETYPE_INFO2(&pa
);
1202 ASN1_MALLOC_ENCODE(ETYPE_INFO2
, buf
, len
, &pa
, &len
, ret
);
1203 free_ETYPE_INFO2(&pa
);
1206 ret
= realloc_method_data(md
);
1211 md
->val
[md
->len
- 1].padata_type
= KRB5_PADATA_ETYPE_INFO2
;
1212 md
->val
[md
->len
- 1].padata_value
.length
= len
;
1213 md
->val
[md
->len
- 1].padata_value
.data
= buf
;
1222 log_as_req(krb5_context context
,
1223 krb5_kdc_configuration
*config
,
1224 krb5_enctype cetype
,
1225 krb5_enctype setype
,
1226 const KDC_REQ_BODY
*b
)
1228 krb5_error_code ret
;
1229 struct rk_strpool
*p
;
1233 p
= rk_strpoolprintf(NULL
, "%s", "Client supported enctypes: ");
1235 for (i
= 0; i
< b
->etype
.len
; i
++) {
1236 ret
= krb5_enctype_to_string(context
, b
->etype
.val
[i
], &str
);
1238 p
= rk_strpoolprintf(p
, "%s", str
);
1241 p
= rk_strpoolprintf(p
, "%d", b
->etype
.val
[i
]);
1242 if (p
&& i
+ 1 < b
->etype
.len
)
1243 p
= rk_strpoolprintf(p
, ", ");
1245 kdc_log(context
, config
, 0, "out of memory");
1250 p
= rk_strpoolprintf(p
, "no encryption types");
1256 ret
= krb5_enctype_to_string(context
, cetype
, &cet
);
1258 ret
= krb5_enctype_to_string(context
, setype
, &set
);
1260 p
= rk_strpoolprintf(p
, ", using %s/%s", cet
, set
);
1266 p
= rk_strpoolprintf(p
, ", using enctypes %d/%d",
1270 str
= rk_strpoolcollect(p
);
1271 kdc_log(context
, config
, 0, "%s", str
);
1276 unparse_flags(KDCOptions2int(b
->kdc_options
), asn1_KDCOptions_units(),
1277 fixedstr
, sizeof(fixedstr
));
1279 kdc_log(context
, config
, 0, "Requested flags: %s", fixedstr
);
1284 * verify the flags on `client' and `server', returning 0
1285 * if they are OK and generating an error messages and returning
1286 * and error code otherwise.
1290 kdc_check_flags(krb5_context context
,
1291 krb5_kdc_configuration
*config
,
1292 hdb_entry_ex
*client_ex
, const char *client_name
,
1293 hdb_entry_ex
*server_ex
, const char *server_name
,
1294 krb5_boolean is_as_req
)
1296 if(client_ex
!= NULL
) {
1297 hdb_entry
*client
= &client_ex
->entry
;
1300 if (client
->flags
.locked_out
) {
1301 kdc_log(context
, config
, 0,
1302 "Client (%s) is locked out", client_name
);
1303 return KRB5KDC_ERR_POLICY
;
1306 if (client
->flags
.invalid
) {
1307 kdc_log(context
, config
, 0,
1308 "Client (%s) has invalid bit set", client_name
);
1309 return KRB5KDC_ERR_POLICY
;
1312 if(!client
->flags
.client
){
1313 kdc_log(context
, config
, 0,
1314 "Principal may not act as client -- %s", client_name
);
1315 return KRB5KDC_ERR_POLICY
;
1318 if (client
->valid_start
&& *client
->valid_start
> kdc_time
) {
1319 char starttime_str
[100];
1320 krb5_format_time(context
, *client
->valid_start
,
1321 starttime_str
, sizeof(starttime_str
), TRUE
);
1322 kdc_log(context
, config
, 0,
1323 "Client not yet valid until %s -- %s",
1324 starttime_str
, client_name
);
1325 return KRB5KDC_ERR_CLIENT_NOTYET
;
1328 if (client
->valid_end
&& *client
->valid_end
< kdc_time
) {
1329 char endtime_str
[100];
1330 krb5_format_time(context
, *client
->valid_end
,
1331 endtime_str
, sizeof(endtime_str
), TRUE
);
1332 kdc_log(context
, config
, 0,
1333 "Client expired at %s -- %s",
1334 endtime_str
, client_name
);
1335 return KRB5KDC_ERR_NAME_EXP
;
1338 if (client
->flags
.require_pwchange
&&
1339 (server_ex
== NULL
|| !server_ex
->entry
.flags
.change_pw
)) {
1340 kdc_log(context
, config
, 0,
1341 "Client's key must be changed -- %s", client_name
);
1342 return KRB5KDC_ERR_KEY_EXPIRED
;
1345 if (client
->pw_end
&& *client
->pw_end
< kdc_time
1346 && (server_ex
== NULL
|| !server_ex
->entry
.flags
.change_pw
)) {
1347 char pwend_str
[100];
1348 krb5_format_time(context
, *client
->pw_end
,
1349 pwend_str
, sizeof(pwend_str
), TRUE
);
1350 kdc_log(context
, config
, 0,
1351 "Client's key has expired at %s -- %s",
1352 pwend_str
, client_name
);
1353 return KRB5KDC_ERR_KEY_EXPIRED
;
1359 if (server_ex
!= NULL
) {
1360 hdb_entry
*server
= &server_ex
->entry
;
1362 if (server
->flags
.locked_out
) {
1363 kdc_log(context
, config
, 0,
1364 "Client server locked out -- %s", server_name
);
1365 return KRB5KDC_ERR_POLICY
;
1367 if (server
->flags
.invalid
) {
1368 kdc_log(context
, config
, 0,
1369 "Server has invalid flag set -- %s", server_name
);
1370 return KRB5KDC_ERR_POLICY
;
1373 if(!server
->flags
.server
){
1374 kdc_log(context
, config
, 0,
1375 "Principal may not act as server -- %s", server_name
);
1376 return KRB5KDC_ERR_POLICY
;
1379 if(!is_as_req
&& server
->flags
.initial
) {
1380 kdc_log(context
, config
, 0,
1381 "AS-REQ is required for server -- %s", server_name
);
1382 return KRB5KDC_ERR_POLICY
;
1385 if (server
->valid_start
&& *server
->valid_start
> kdc_time
) {
1386 char starttime_str
[100];
1387 krb5_format_time(context
, *server
->valid_start
,
1388 starttime_str
, sizeof(starttime_str
), TRUE
);
1389 kdc_log(context
, config
, 0,
1390 "Server not yet valid until %s -- %s",
1391 starttime_str
, server_name
);
1392 return KRB5KDC_ERR_SERVICE_NOTYET
;
1395 if (server
->valid_end
&& *server
->valid_end
< kdc_time
) {
1396 char endtime_str
[100];
1397 krb5_format_time(context
, *server
->valid_end
,
1398 endtime_str
, sizeof(endtime_str
), TRUE
);
1399 kdc_log(context
, config
, 0,
1400 "Server expired at %s -- %s",
1401 endtime_str
, server_name
);
1402 return KRB5KDC_ERR_SERVICE_EXP
;
1405 if (server
->pw_end
&& *server
->pw_end
< kdc_time
) {
1406 char pwend_str
[100];
1407 krb5_format_time(context
, *server
->pw_end
,
1408 pwend_str
, sizeof(pwend_str
), TRUE
);
1409 kdc_log(context
, config
, 0,
1410 "Server's key has expired at -- %s",
1411 pwend_str
, server_name
);
1412 return KRB5KDC_ERR_KEY_EXPIRED
;
1419 * Return TRUE if `from' is part of `addresses' taking into consideration
1420 * the configuration variables that tells us how strict we should be about
1425 _kdc_check_addresses(krb5_context context
,
1426 krb5_kdc_configuration
*config
,
1427 HostAddresses
*addresses
, const struct sockaddr
*from
)
1429 krb5_error_code ret
;
1431 krb5_boolean result
;
1432 krb5_boolean only_netbios
= TRUE
;
1435 if(config
->check_ticket_addresses
== 0)
1438 if(addresses
== NULL
)
1439 return config
->allow_null_ticket_addresses
;
1441 for (i
= 0; i
< addresses
->len
; ++i
) {
1442 if (addresses
->val
[i
].addr_type
!= KRB5_ADDRESS_NETBIOS
) {
1443 only_netbios
= FALSE
;
1447 /* Windows sends it's netbios name, which I can only assume is
1448 * used for the 'allowed workstations' check. This is painful,
1449 * but we still want to check IP addresses if they happen to be
1454 return config
->allow_null_ticket_addresses
;
1456 ret
= krb5_sockaddr2address (context
, from
, &addr
);
1460 result
= krb5_address_search(context
, &addr
, addresses
);
1461 krb5_free_address (context
, &addr
);
1470 send_pac_p(krb5_context context
, KDC_REQ
*req
)
1472 krb5_error_code ret
;
1473 PA_PAC_REQUEST pacreq
;
1477 pa
= _kdc_find_padata(req
, &i
, KRB5_PADATA_PA_PAC_REQUEST
);
1481 ret
= decode_PA_PAC_REQUEST(pa
->padata_value
.data
,
1482 pa
->padata_value
.length
,
1487 i
= pacreq
.include_pac
;
1488 free_PA_PAC_REQUEST(&pacreq
);
1498 static krb5_error_code
1499 generate_pac(kdc_request_t r
, Key
*skey
)
1501 krb5_error_code ret
;
1505 ret
= _kdc_pac_generate(r
->context
, r
->client
, &p
);
1507 _kdc_r_log(r
, 0, "PAC generation failed for -- %s",
1514 ret
= _krb5_pac_sign(r
->context
, p
, r
->et
.authtime
,
1515 r
->client
->entry
.principal
,
1516 &skey
->key
, /* Server key */
1517 &skey
->key
, /* FIXME: should be krbtgt key */
1519 krb5_pac_free(r
->context
, p
);
1521 _kdc_r_log(r
, 0, "PAC signing failed for -- %s",
1526 ret
= _kdc_tkt_add_if_relevant_ad(r
->context
, &r
->et
,
1527 KRB5_AUTHDATA_WIN2K_PAC
,
1529 krb5_data_free(&data
);
1539 _kdc_is_anonymous(krb5_context context
, krb5_principal principal
)
1541 if (principal
->name
.name_type
!= KRB5_NT_WELLKNOWN
||
1542 principal
->name
.name_string
.len
!= 2 ||
1543 strcmp(principal
->name
.name_string
.val
[0], KRB5_WELLKNOWN_NAME
) != 0 ||
1544 strcmp(principal
->name
.name_string
.val
[1], KRB5_ANON_NAME
) != 0)
1550 require_preauth_p(kdc_request_t r
)
1552 return r
->config
->require_preauth
1553 || r
->client
->entry
.flags
.require_preauth
1554 || r
->server
->entry
.flags
.require_preauth
;
1562 static krb5_error_code
1563 add_enc_pa_rep(kdc_request_t r
)
1565 krb5_error_code ret
;
1571 ret
= krb5_crypto_init(r
->context
, &r
->reply_key
, 0, &crypto
);
1575 ret
= krb5_create_checksum(r
->context
, crypto
,
1577 r
->request
.data
, r
->request
.length
,
1579 krb5_crypto_destroy(r
->context
, crypto
);
1583 ASN1_MALLOC_ENCODE(Checksum
, cdata
.data
, cdata
.length
,
1584 &checksum
, &len
, ret
);
1585 free_Checksum(&checksum
);
1588 heim_assert(cdata
.length
== len
, "ASN.1 internal error");
1590 if (r
->ek
.encrypted_pa_data
== NULL
) {
1591 ALLOC(r
->ek
.encrypted_pa_data
);
1592 if (r
->ek
.encrypted_pa_data
== NULL
)
1595 ret
= krb5_padata_add(r
->context
, r
->ek
.encrypted_pa_data
,
1596 KRB5_PADATA_REQ_ENC_PA_REP
, cdata
.data
, cdata
.length
);
1600 return krb5_padata_add(r
->context
, r
->ek
.encrypted_pa_data
,
1601 KRB5_PADATA_FX_FAST
, NULL
, 0);
1609 _kdc_as_rep(kdc_request_t r
,
1612 struct sockaddr
*from_addr
,
1615 krb5_context context
= r
->context
;
1616 krb5_kdc_configuration
*config
= r
->config
;
1617 KDC_REQ
*req
= &r
->req
;
1618 KDC_REQ_BODY
*b
= NULL
;
1621 krb5_enctype setype
;
1622 krb5_error_code ret
= 0;
1625 int i
, flags
= HDB_F_FOR_AS_REQ
;
1626 METHOD_DATA error_method
;
1629 memset(&rep
, 0, sizeof(rep
));
1630 error_method
.len
= 0;
1631 error_method
.val
= NULL
;
1634 * Look for FAST armor and unwrap
1636 ret
= _kdc_fast_unwrap_request(r
);
1638 _kdc_r_log(r
, 0, "FAST unwrap request from %s failed: %d", from
, ret
);
1646 flags
|= HDB_F_CANON
;
1648 if(b
->sname
== NULL
){
1649 ret
= KRB5KRB_ERR_GENERIC
;
1650 _kdc_set_e_text(r
, "No server in request");
1652 ret
= _krb5_principalname2krb5_principal (context
,
1657 ret
= krb5_unparse_name(context
, r
->server_princ
, &r
->server_name
);
1660 kdc_log(context
, config
, 0,
1661 "AS-REQ malformed server name from %s", from
);
1664 if(b
->cname
== NULL
){
1665 ret
= KRB5KRB_ERR_GENERIC
;
1666 _kdc_set_e_text(r
, "No client in request");
1668 ret
= _krb5_principalname2krb5_principal (context
,
1675 ret
= krb5_unparse_name(context
, r
->client_princ
, &r
->client_name
);
1678 kdc_log(context
, config
, 0,
1679 "AS-REQ malformed client name from %s", from
);
1683 kdc_log(context
, config
, 0, "AS-REQ %s from %s for %s",
1684 r
->client_name
, from
, r
->server_name
);
1690 if (_kdc_is_anonymous(context
, r
->client_princ
)) {
1691 if (!_kdc_is_anon_request(b
)) {
1692 kdc_log(context
, config
, 0, "Anonymous ticket w/o anonymous flag");
1693 ret
= KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN
;
1696 } else if (_kdc_is_anon_request(b
)) {
1697 kdc_log(context
, config
, 0,
1698 "Request for a anonymous ticket with non "
1699 "anonymous client name: %s", r
->client_name
);
1700 ret
= KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN
;
1708 ret
= _kdc_db_fetch(context
, config
, r
->client_princ
,
1709 HDB_F_GET_CLIENT
| flags
, NULL
,
1710 &r
->clientdb
, &r
->client
);
1711 if(ret
== HDB_ERR_NOT_FOUND_HERE
) {
1712 kdc_log(context
, config
, 5, "client %s does not have secrets at this KDC, need to proxy",
1715 } else if (ret
== HDB_ERR_WRONG_REALM
) {
1716 char *fixed_client_name
= NULL
;
1718 ret
= krb5_unparse_name(context
, r
->client
->entry
.principal
,
1719 &fixed_client_name
);
1724 kdc_log(context
, config
, 0, "WRONG_REALM - %s -> %s",
1725 r
->client_name
, fixed_client_name
);
1726 free(fixed_client_name
);
1728 ret
= _kdc_fast_mk_error(context
, r
,
1732 KRB5_KDC_ERR_WRONG_REALM
,
1736 &r
->client
->entry
.principal
->realm
,
1741 const char *msg
= krb5_get_error_message(context
, ret
);
1742 kdc_log(context
, config
, 0, "UNKNOWN -- %s: %s", r
->client_name
, msg
);
1743 krb5_free_error_message(context
, msg
);
1744 ret
= KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN
;
1747 ret
= _kdc_db_fetch(context
, config
, r
->server_princ
,
1748 HDB_F_GET_SERVER
|HDB_F_GET_KRBTGT
| flags
,
1749 NULL
, NULL
, &r
->server
);
1750 if(ret
== HDB_ERR_NOT_FOUND_HERE
) {
1751 kdc_log(context
, config
, 5, "target %s does not have secrets at this KDC, need to proxy",
1755 const char *msg
= krb5_get_error_message(context
, ret
);
1756 kdc_log(context
, config
, 0, "UNKNOWN -- %s: %s", r
->server_name
, msg
);
1757 krb5_free_error_message(context
, msg
);
1758 ret
= KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN
;
1763 * Select a session enctype from the list of the crypto system
1764 * supported enctypes that is supported by the client and is one of
1765 * the enctype of the enctype of the service (likely krbtgt).
1767 * The latter is used as a hint of what enctypes all KDC support,
1768 * to make sure a newer version of KDC won't generate a session
1769 * enctype that an older version of a KDC in the same realm can't
1773 ret
= _kdc_find_etype(context
,
1774 krb5_principal_is_krbtgt(context
, r
->server_princ
) ?
1775 config
->tgt_use_strongest_session_key
:
1776 config
->svc_use_strongest_session_key
, FALSE
,
1777 r
->client
, b
->etype
.val
, b
->etype
.len
, &r
->sessionetype
,
1780 kdc_log(context
, config
, 0,
1781 "Client (%s) from %s has no common enctypes with KDC "
1782 "to use for the session key",
1783 r
->client_name
, from
);
1788 * Pre-auth processing
1794 log_patypes(context
, config
, req
->padata
);
1796 /* Check if preauth matching */
1798 for (n
= 0; !found_pa
&& n
< sizeof(pat
) / sizeof(pat
[0]); n
++) {
1799 if (pat
[n
].validate
== NULL
)
1801 if (r
->armor_crypto
== NULL
&& (pat
[n
].flags
& PA_REQ_FAST
))
1804 kdc_log(context
, config
, 5,
1805 "Looking for %s pa-data -- %s", pat
[n
].name
, r
->client_name
);
1807 pa
= _kdc_find_padata(req
, &i
, pat
[n
].type
);
1809 ret
= pat
[n
].validate(r
, pa
);
1813 kdc_log(context
, config
, 0,
1814 "%s pre-authentication succeeded -- %s",
1815 pat
[n
].name
, r
->client_name
);
1817 r
->et
.flags
.pre_authent
= 1;
1822 if (found_pa
== 0) {
1826 for (n
= 0; n
< sizeof(pat
) / sizeof(pat
[0]); n
++) {
1827 if ((pat
[n
].flags
& PA_ANNOUNCE
) == 0)
1829 ret
= krb5_padata_add(context
, &error_method
,
1830 pat
[n
].type
, NULL
, 0);
1836 * If there is a client key, send ETYPE_INFO{,2}
1838 ret
= _kdc_find_etype(context
,
1839 config
->preauth_use_strongest_session_key
, TRUE
,
1840 r
->client
, b
->etype
.val
, b
->etype
.len
, NULL
, &ckey
);
1845 * - If the client only knows about old enctypes, then send
1846 * both info replies (we send 'info' first in the list).
1847 * - If the client is 'modern', because it knows about 'new'
1848 * enctype types, then only send the 'info2' reply.
1850 * Before we send the full list of etype-info data, we pick
1851 * the client key we would have used anyway below, just pick
1855 if (older_enctype(ckey
->key
.keytype
)) {
1856 ret
= get_pa_etype_info(context
, config
,
1857 &error_method
, ckey
);
1861 ret
= get_pa_etype_info2(context
, config
,
1862 &error_method
, ckey
);
1868 * send requre preauth is its required or anon is requested,
1869 * anon is today only allowed via preauth mechanisms.
1871 if (require_preauth_p(r
) || _kdc_is_anon_request(b
)) {
1872 ret
= KRB5KDC_ERR_PREAUTH_REQUIRED
;
1873 _kdc_set_e_text(r
, "Need to use PA-ENC-TIMESTAMP/PA-PK-AS-REQ");
1878 ret
= KRB5KDC_ERR_CLIENT_NOTYET
;
1879 _kdc_set_e_text(r
, "Doesn't have a client key available");
1882 krb5_free_keyblock_contents(r
->context
, &r
->reply_key
);
1883 ret
= krb5_copy_keyblock_contents(r
->context
, &ckey
->key
, &r
->reply_key
);
1888 if (r
->clientdb
->hdb_auth_status
) {
1889 r
->clientdb
->hdb_auth_status(context
, r
->clientdb
, r
->client
,
1894 * Verify flags after the user been required to prove its identity
1895 * with in a preauth mech.
1898 ret
= _kdc_check_access(context
, config
, r
->client
, r
->client_name
,
1899 r
->server
, r
->server_name
,
1900 req
, &error_method
);
1905 * Select the best encryption type for the KDC with out regard to
1906 * the client since the client never needs to read that data.
1909 ret
= _kdc_get_preferred_key(context
, config
,
1910 r
->server
, r
->server_name
,
1915 if(f
.renew
|| f
.validate
|| f
.proxy
|| f
.forwarded
|| f
.enc_tkt_in_skey
1916 || (_kdc_is_anon_request(b
) && !config
->allow_anonymous
)) {
1917 ret
= KRB5KDC_ERR_BADOPTION
;
1918 _kdc_set_e_text(r
, "Bad KDC options");
1927 rep
.msg_type
= krb_as_rep
;
1929 if (_kdc_is_anonymous(context
, r
->client_princ
)) {
1930 Realm anon_realm
=KRB5_ANON_REALM
;
1931 ret
= copy_Realm(&anon_realm
, &rep
.crealm
);
1933 ret
= copy_Realm(&r
->client
->entry
.principal
->realm
, &rep
.crealm
);
1936 ret
= _krb5_principal2principalname(&rep
.cname
, r
->client
->entry
.principal
);
1940 rep
.ticket
.tkt_vno
= 5;
1941 copy_Realm(&r
->server
->entry
.principal
->realm
, &rep
.ticket
.realm
);
1942 _krb5_principal2principalname(&rep
.ticket
.sname
,
1943 r
->server
->entry
.principal
);
1944 /* java 1.6 expects the name to be the same type, lets allow that
1945 * uncomplicated name-types. */
1946 #define CNT(sp,t) (((sp)->sname->name_type) == KRB5_NT_##t)
1947 if (CNT(b
, UNKNOWN
) || CNT(b
, PRINCIPAL
) || CNT(b
, SRV_INST
) || CNT(b
, SRV_HST
) || CNT(b
, SRV_XHST
))
1948 rep
.ticket
.sname
.name_type
= b
->sname
->name_type
;
1951 r
->et
.flags
.initial
= 1;
1952 if(r
->client
->entry
.flags
.forwardable
&& r
->server
->entry
.flags
.forwardable
)
1953 r
->et
.flags
.forwardable
= f
.forwardable
;
1954 else if (f
.forwardable
) {
1955 _kdc_set_e_text(r
, "Ticket may not be forwardable");
1956 ret
= KRB5KDC_ERR_POLICY
;
1959 if(r
->client
->entry
.flags
.proxiable
&& r
->server
->entry
.flags
.proxiable
)
1960 r
->et
.flags
.proxiable
= f
.proxiable
;
1961 else if (f
.proxiable
) {
1962 _kdc_set_e_text(r
, "Ticket may not be proxiable");
1963 ret
= KRB5KDC_ERR_POLICY
;
1966 if(r
->client
->entry
.flags
.postdate
&& r
->server
->entry
.flags
.postdate
)
1967 r
->et
.flags
.may_postdate
= f
.allow_postdate
;
1968 else if (f
.allow_postdate
){
1969 _kdc_set_e_text(r
, "Ticket may not be postdate");
1970 ret
= KRB5KDC_ERR_POLICY
;
1974 /* check for valid set of addresses */
1975 if(!_kdc_check_addresses(context
, config
, b
->addresses
, from_addr
)) {
1976 _kdc_set_e_text(r
, "Bad address list in requested");
1977 ret
= KRB5KRB_AP_ERR_BADADDR
;
1981 ret
= copy_PrincipalName(&rep
.cname
, &r
->et
.cname
);
1984 ret
= copy_Realm(&rep
.crealm
, &r
->et
.crealm
);
1992 start
= r
->et
.authtime
= kdc_time
;
1994 if(f
.postdated
&& req
->req_body
.from
){
1995 ALLOC(r
->et
.starttime
);
1996 start
= *r
->et
.starttime
= *req
->req_body
.from
;
1997 r
->et
.flags
.invalid
= 1;
1998 r
->et
.flags
.postdated
= 1; /* XXX ??? */
2000 _kdc_fix_time(&b
->till
);
2003 /* be careful not overflowing */
2005 if(r
->client
->entry
.max_life
)
2006 t
= start
+ min(t
- start
, *r
->client
->entry
.max_life
);
2007 if(r
->server
->entry
.max_life
)
2008 t
= start
+ min(t
- start
, *r
->server
->entry
.max_life
);
2010 t
= min(t
, start
+ realm
->max_life
);
2013 if(f
.renewable_ok
&& r
->et
.endtime
< *b
->till
){
2015 if(b
->rtime
== NULL
){
2019 if(*b
->rtime
< *b
->till
)
2020 *b
->rtime
= *b
->till
;
2022 if(f
.renewable
&& b
->rtime
){
2026 if(r
->client
->entry
.max_renew
)
2027 t
= start
+ min(t
- start
, *r
->client
->entry
.max_renew
);
2028 if(r
->server
->entry
.max_renew
)
2029 t
= start
+ min(t
- start
, *r
->server
->entry
.max_renew
);
2031 t
= min(t
, start
+ realm
->max_renew
);
2033 ALLOC(r
->et
.renew_till
);
2034 *r
->et
.renew_till
= t
;
2035 r
->et
.flags
.renewable
= 1;
2039 if (_kdc_is_anon_request(b
))
2040 r
->et
.flags
.anonymous
= 1;
2044 copy_HostAddresses(b
->addresses
, r
->et
.caddr
);
2047 r
->et
.transited
.tr_type
= DOMAIN_X500_COMPRESS
;
2048 krb5_data_zero(&r
->et
.transited
.contents
);
2050 /* The MIT ASN.1 library (obviously) doesn't tell lengths encoded
2051 * as 0 and as 0x80 (meaning indefinite length) apart, and is thus
2052 * incapable of correctly decoding SEQUENCE OF's of zero length.
2054 * To fix this, always send at least one no-op last_req
2056 * If there's a pw_end or valid_end we will use that,
2057 * otherwise just a dummy lr.
2059 r
->ek
.last_req
.val
= malloc(2 * sizeof(*r
->ek
.last_req
.val
));
2060 if (r
->ek
.last_req
.val
== NULL
) {
2064 r
->ek
.last_req
.len
= 0;
2065 if (r
->client
->entry
.pw_end
2066 && (config
->kdc_warn_pwexpire
== 0
2067 || kdc_time
+ config
->kdc_warn_pwexpire
>= *r
->client
->entry
.pw_end
)) {
2068 r
->ek
.last_req
.val
[r
->ek
.last_req
.len
].lr_type
= LR_PW_EXPTIME
;
2069 r
->ek
.last_req
.val
[r
->ek
.last_req
.len
].lr_value
= *r
->client
->entry
.pw_end
;
2070 ++r
->ek
.last_req
.len
;
2072 if (r
->client
->entry
.valid_end
) {
2073 r
->ek
.last_req
.val
[r
->ek
.last_req
.len
].lr_type
= LR_ACCT_EXPTIME
;
2074 r
->ek
.last_req
.val
[r
->ek
.last_req
.len
].lr_value
= *r
->client
->entry
.valid_end
;
2075 ++r
->ek
.last_req
.len
;
2077 if (r
->ek
.last_req
.len
== 0) {
2078 r
->ek
.last_req
.val
[r
->ek
.last_req
.len
].lr_type
= LR_NONE
;
2079 r
->ek
.last_req
.val
[r
->ek
.last_req
.len
].lr_value
= 0;
2080 ++r
->ek
.last_req
.len
;
2082 r
->ek
.nonce
= b
->nonce
;
2083 if (r
->client
->entry
.valid_end
|| r
->client
->entry
.pw_end
) {
2084 ALLOC(r
->ek
.key_expiration
);
2085 if (r
->client
->entry
.valid_end
) {
2086 if (r
->client
->entry
.pw_end
)
2087 *r
->ek
.key_expiration
= min(*r
->client
->entry
.valid_end
,
2088 *r
->client
->entry
.pw_end
);
2090 *r
->ek
.key_expiration
= *r
->client
->entry
.valid_end
;
2092 *r
->ek
.key_expiration
= *r
->client
->entry
.pw_end
;
2094 r
->ek
.key_expiration
= NULL
;
2095 r
->ek
.flags
= r
->et
.flags
;
2096 r
->ek
.authtime
= r
->et
.authtime
;
2097 if (r
->et
.starttime
) {
2098 ALLOC(r
->ek
.starttime
);
2099 *r
->ek
.starttime
= *r
->et
.starttime
;
2101 r
->ek
.endtime
= r
->et
.endtime
;
2102 if (r
->et
.renew_till
) {
2103 ALLOC(r
->ek
.renew_till
);
2104 *r
->ek
.renew_till
= *r
->et
.renew_till
;
2106 copy_Realm(&rep
.ticket
.realm
, &r
->ek
.srealm
);
2107 copy_PrincipalName(&rep
.ticket
.sname
, &r
->ek
.sname
);
2110 copy_HostAddresses(r
->et
.caddr
, r
->ek
.caddr
);
2114 * Check and session and reply keys
2117 if (r
->session_key
.keytype
== ETYPE_NULL
) {
2118 ret
= krb5_generate_random_keyblock(context
, r
->sessionetype
, &r
->session_key
);
2123 if (r
->reply_key
.keytype
== ETYPE_NULL
) {
2124 _kdc_set_e_text(r
, "Client have no reply key");
2125 ret
= KRB5KDC_ERR_CLIENT_NOTYET
;
2129 ret
= copy_EncryptionKey(&r
->session_key
, &r
->et
.key
);
2133 ret
= copy_EncryptionKey(&r
->session_key
, &r
->ek
.key
);
2137 if (r
->outpadata
.len
) {
2140 if (rep
.padata
== NULL
) {
2144 ret
= copy_METHOD_DATA(&r
->outpadata
, rep
.padata
);
2150 if (send_pac_p(context
, req
)) {
2151 generate_pac(r
, skey
);
2154 _kdc_log_timestamp(context
, config
, "AS-REQ", r
->et
.authtime
, r
->et
.starttime
,
2155 r
->et
.endtime
, r
->et
.renew_till
);
2157 /* do this as the last thing since this signs the EncTicketPart */
2158 ret
= _kdc_add_KRB5SignedPath(context
,
2162 r
->client
->entry
.principal
,
2169 log_as_req(context
, config
, r
->reply_key
.keytype
, setype
, b
);
2172 * We always say we support FAST/enc-pa-rep
2175 r
->et
.flags
.enc_pa_rep
= r
->ek
.flags
.enc_pa_rep
= 1;
2178 * Add REQ_ENC_PA_REP if client supports it
2182 pa
= _kdc_find_padata(req
, &i
, KRB5_PADATA_REQ_ENC_PA_REP
);
2185 ret
= add_enc_pa_rep(r
);
2187 const char *msg
= krb5_get_error_message(r
->context
, ret
);
2188 _kdc_r_log(r
, 0, "add_enc_pa_rep failed: %s: %d", msg
, ret
);
2189 krb5_free_error_message(r
->context
, msg
);
2198 ret
= _kdc_encode_reply(context
, config
,
2199 r
->armor_crypto
, req
->req_body
.nonce
,
2200 &rep
, &r
->et
, &r
->ek
, setype
, r
->server
->entry
.kvno
,
2201 &skey
->key
, r
->client
->entry
.kvno
,
2202 &r
->reply_key
, 0, &r
->e_text
, reply
);
2207 * Check if message too large
2209 if (datagram_reply
&& reply
->length
> config
->max_datagram_reply_length
) {
2210 krb5_data_free(reply
);
2211 ret
= KRB5KRB_ERR_RESPONSE_TOO_BIG
;
2212 _kdc_set_e_text(r
, "Reply packet too large");
2219 * In case of a non proxy error, build an error message.
2221 if(ret
!= 0 && ret
!= HDB_ERR_NOT_FOUND_HERE
&& reply
->length
== 0) {
2222 ret
= _kdc_fast_mk_error(context
, r
,
2228 &r
->client_princ
->name
,
2229 &r
->client_princ
->realm
,
2236 free_EncTicketPart(&r
->et
);
2237 free_EncKDCRepPart(&r
->ek
);
2238 free_KDCFastState(&r
->fast
);
2240 if (error_method
.len
)
2241 free_METHOD_DATA(&error_method
);
2242 if (r
->outpadata
.len
)
2243 free_METHOD_DATA(&r
->outpadata
);
2244 if (r
->client_princ
) {
2245 krb5_free_principal(context
, r
->client_princ
);
2246 r
->client_princ
= NULL
;
2248 if (r
->client_name
) {
2249 free(r
->client_name
);
2250 r
->client_name
= NULL
;
2252 if (r
->server_princ
){
2253 krb5_free_principal(context
, r
->server_princ
);
2254 r
->server_princ
= NULL
;
2256 if (r
->server_name
) {
2257 free(r
->server_name
);
2258 r
->server_name
= NULL
;
2261 _kdc_free_ent(context
, r
->client
);
2263 _kdc_free_ent(context
, r
->server
);
2264 if (r
->armor_crypto
) {
2265 krb5_crypto_destroy(r
->context
, r
->armor_crypto
);
2266 r
->armor_crypto
= NULL
;
2268 krb5_free_keyblock_contents(r
->context
, &r
->reply_key
);
2269 krb5_free_keyblock_contents(r
->context
, &r
->session_key
);
2274 * Add the AuthorizationData `data´ of `type´ to the last element in
2275 * the sequence of authorization_data in `tkt´ wrapped in an IF_RELEVANT
2279 _kdc_tkt_add_if_relevant_ad(krb5_context context
,
2282 const krb5_data
*data
)
2284 krb5_error_code ret
;
2287 if (tkt
->authorization_data
== NULL
) {
2288 tkt
->authorization_data
= calloc(1, sizeof(*tkt
->authorization_data
));
2289 if (tkt
->authorization_data
== NULL
) {
2290 krb5_set_error_message(context
, ENOMEM
, "out of memory");
2295 /* add the entry to the last element */
2297 AuthorizationData ad
= { 0, NULL
};
2298 AuthorizationDataElement ade
;
2301 ade
.ad_data
= *data
;
2303 ret
= add_AuthorizationData(&ad
, &ade
);
2305 krb5_set_error_message(context
, ret
, "add AuthorizationData failed");
2309 ade
.ad_type
= KRB5_AUTHDATA_IF_RELEVANT
;
2311 ASN1_MALLOC_ENCODE(AuthorizationData
,
2312 ade
.ad_data
.data
, ade
.ad_data
.length
,
2314 free_AuthorizationData(&ad
);
2316 krb5_set_error_message(context
, ret
, "ASN.1 encode of "
2317 "AuthorizationData failed");
2320 if (ade
.ad_data
.length
!= size
)
2321 krb5_abortx(context
, "internal asn.1 encoder error");
2323 ret
= add_AuthorizationData(tkt
->authorization_data
, &ade
);
2324 der_free_octet_string(&ade
.ad_data
);
2326 krb5_set_error_message(context
, ret
, "add AuthorizationData failed");
2335 _kdc_is_anon_request(const KDC_REQ_BODY
*b
)
2337 /* some versions of heimdal use bit 14 instead of 16 for
2338 request_anonymous, as indicated in the anonymous draft prior to
2339 version 11. Bit 14 is assigned to S4U2Proxy, but all S4U2Proxy
2340 requests will have a second ticket; don't consider those anonymous */
2341 return (b
->kdc_options
.request_anonymous
||
2342 (b
->kdc_options
.constrained_delegation
&& !b
->additional_tickets
));