2 * Copyright (c) 1997-2011 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Portions Copyright (c) 2010 - 2011 Apple Inc. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 static krb5_error_code
39 get_fastuser_crypto(kdc_request_t r
, krb5_enctype enctype
, krb5_crypto
*crypto
)
41 krb5_principal fast_princ
;
42 hdb_entry_ex
*fast_user
= NULL
;
43 Key
*cookie_key
= NULL
;
48 ret
= krb5_make_principal(r
->context
, &fast_princ
,
49 KRB5_WELLKNOWN_ORG_H5L_REALM
,
50 KRB5_WELLKNOWN_NAME
, "org.h5l.fast-cookie", NULL
);
54 ret
= _kdc_db_fetch(r
->context
, r
->config
, fast_princ
,
55 HDB_F_GET_CLIENT
, NULL
, NULL
, &fast_user
);
56 krb5_free_principal(r
->context
, fast_princ
);
60 if (enctype
== KRB5_ENCTYPE_NULL
)
61 ret
= _kdc_get_preferred_key(r
->context
, r
->config
, fast_user
,
62 "fast-cookie", &enctype
, &cookie_key
);
64 ret
= hdb_enctype2key(r
->context
, &fast_user
->entry
, NULL
,
65 enctype
, &cookie_key
);
69 ret
= krb5_crypto_init(r
->context
, &cookie_key
->key
, 0, crypto
);
75 _kdc_free_ent(r
->context
, fast_user
);
81 static krb5_error_code
82 fast_parse_cookie(kdc_request_t r
, const PA_DATA
*pa
)
84 krb5_crypto crypto
= NULL
;
90 ret
= decode_KDCFastCookie(pa
->padata_value
.data
,
91 pa
->padata_value
.length
,
96 if (len
!= pa
->padata_value
.length
|| strcmp("H5L1", data
.version
) != 0) {
97 free_KDCFastCookie(&data
);
98 return KRB5KDC_ERR_POLICY
;
101 ret
= get_fastuser_crypto(r
, data
.cookie
.etype
, &crypto
);
105 ret
= krb5_decrypt_EncryptedData(r
->context
, crypto
,
108 krb5_crypto_destroy(r
->context
, crypto
);
112 ret
= decode_KDCFastState(d1
.data
, d1
.length
, &r
->fast
, &len
);
117 if (r
->fast
.expiration
< kdc_time
) {
118 kdc_log(r
->context
, r
->config
, 0, "fast cookie expired");
119 ret
= KRB5KDC_ERR_POLICY
;
124 free_KDCFastCookie(&data
);
129 static krb5_error_code
130 fast_add_cookie(kdc_request_t r
, METHOD_DATA
*method_data
)
132 krb5_crypto crypto
= NULL
;
138 memset(&shell
, 0, sizeof(shell
));
140 r
->fast
.expiration
= kdc_time
+ FAST_EXPIRATION_TIME
;
142 ASN1_MALLOC_ENCODE(KDCFastState
, data
.data
, data
.length
,
143 &r
->fast
, &size
, ret
);
146 heim_assert(size
== data
.length
, "internal asn1 encoder error");
148 ret
= get_fastuser_crypto(r
, KRB5_ENCTYPE_NULL
, &crypto
);
152 ret
= krb5_encrypt_EncryptedData(r
->context
, crypto
,
154 data
.data
, data
.length
, 0,
156 krb5_crypto_destroy(r
->context
, crypto
);
162 shell
.version
= "H5L1";
164 ASN1_MALLOC_ENCODE(KDCFastCookie
, data
.data
, data
.length
,
166 free_EncryptedData(&shell
.cookie
);
169 heim_assert(size
== data
.length
, "internal asn1 encoder error");
171 ret
= krb5_padata_add(r
->context
, method_data
,
172 KRB5_PADATA_FX_COOKIE
,
173 data
.data
, data
.length
);
181 _kdc_fast_mk_response(krb5_context context
,
182 krb5_crypto armor_crypto
,
183 METHOD_DATA
*pa_data
,
184 krb5_keyblock
*strengthen_key
,
185 KrbFastFinished
*finished
,
189 PA_FX_FAST_REPLY fxfastrep
;
190 KrbFastResponse fastrep
;
195 memset(&fxfastrep
, 0, sizeof(fxfastrep
));
196 memset(&fastrep
, 0, sizeof(fastrep
));
197 krb5_data_zero(data
);
200 fastrep
.padata
.val
= pa_data
->val
;
201 fastrep
.padata
.len
= pa_data
->len
;
203 fastrep
.strengthen_key
= strengthen_key
;
204 fastrep
.finished
= finished
;
205 fastrep
.nonce
= nonce
;
207 ASN1_MALLOC_ENCODE(KrbFastResponse
, buf
.data
, buf
.length
,
208 &fastrep
, &size
, ret
);
211 if (buf
.length
!= size
)
212 krb5_abortx(context
, "internal asn.1 error");
214 fxfastrep
.element
= choice_PA_FX_FAST_REPLY_armored_data
;
216 ret
= krb5_encrypt_EncryptedData(context
,
222 &fxfastrep
.u
.armored_data
.enc_fast_rep
);
223 krb5_data_free(&buf
);
227 ASN1_MALLOC_ENCODE(PA_FX_FAST_REPLY
, data
->data
, data
->length
,
228 &fxfastrep
, &size
, ret
);
229 free_PA_FX_FAST_REPLY(&fxfastrep
);
232 if (data
->length
!= size
)
233 krb5_abortx(context
, "internal asn.1 error");
240 _kdc_fast_mk_error(krb5_context context
,
242 METHOD_DATA
*error_method
,
243 krb5_crypto armor_crypto
,
244 const KDC_REQ_BODY
*req_body
,
245 krb5_error_code outer_error
,
247 krb5_principal error_server
,
248 const PrincipalName
*error_client_name
,
249 const Realm
*error_client_realm
,
250 time_t *csec
, int *cusec
,
251 krb5_data
*error_msg
)
257 krb5_data_zero(&e_data
);
260 PA_FX_FAST_REPLY fxfastrep
;
261 KrbFastResponse fastrep
;
263 memset(&fxfastrep
, 0, sizeof(fxfastrep
));
264 memset(&fastrep
, 0, sizeof(fastrep
));
266 /* first add the KRB-ERROR to the fast errors */
268 ret
= krb5_mk_error_ext(context
,
281 ret
= krb5_padata_add(context
, error_method
,
282 KRB5_PADATA_FX_ERROR
,
283 e_data
.data
, e_data
.length
);
285 krb5_data_free(&e_data
);
289 if (/* hide_principal */ 0) {
290 error_client_name
= NULL
;
291 error_client_realm
= NULL
;
297 ret
= fast_add_cookie(r
, error_method
);
299 ret
= krb5_padata_add(context
, error_method
,
300 KRB5_PADATA_FX_COOKIE
,
303 kdc_log(r
->context
, r
->config
, 0, "failed to add fast cookie with: %d", ret
);
304 free_METHOD_DATA(error_method
);
308 ret
= _kdc_fast_mk_response(context
, armor_crypto
,
309 error_method
, NULL
, NULL
,
310 req_body
->nonce
, &e_data
);
311 free_METHOD_DATA(error_method
);
315 ret
= krb5_padata_add(context
, error_method
,
317 e_data
.data
, e_data
.length
);
322 if (error_method
&& error_method
->len
) {
323 ASN1_MALLOC_ENCODE(METHOD_DATA
, e_data
.data
, e_data
.length
,
324 error_method
, &size
, ret
);
327 if (e_data
.length
!= size
)
328 krb5_abortx(context
, "internal asn.1 error");
331 ret
= krb5_mk_error_ext(context
,
334 (e_data
.length
? &e_data
: NULL
),
341 krb5_data_free(&e_data
);
347 _kdc_fast_unwrap_request(kdc_request_t r
)
349 krb5_principal armor_server
= NULL
;
350 hdb_entry_ex
*armor_user
= NULL
;
351 PA_FX_FAST_REQUEST fxreq
;
352 krb5_auth_context ac
= NULL
;
353 krb5_ticket
*ticket
= NULL
;
354 krb5_flags ap_req_options
;
355 Key
*armor_key
= NULL
;
356 krb5_keyblock armorkey
;
367 * First look for FX_COOKIE and and process it
369 pa
= _kdc_find_padata(&r
->req
, &i
, KRB5_PADATA_FX_COOKIE
);
371 ret
= fast_parse_cookie(r
, pa
);
377 pa
= _kdc_find_padata(&r
->req
, &i
, KRB5_PADATA_FX_FAST
);
381 ret
= decode_PA_FX_FAST_REQUEST(pa
->padata_value
.data
,
382 pa
->padata_value
.length
,
387 if (len
!= pa
->padata_value
.length
) {
388 ret
= KRB5KDC_ERR_PREAUTH_FAILED
;
392 if (fxreq
.element
!= choice_PA_FX_FAST_REQUEST_armored_data
) {
393 kdc_log(r
->context
, r
->config
, 0,
394 "AS-REQ FAST contain unknown type: %d", (int)fxreq
.element
);
395 ret
= KRB5KDC_ERR_PREAUTH_FAILED
;
399 /* pull out armor key */
400 if (fxreq
.u
.armored_data
.armor
== NULL
) {
401 kdc_log(r
->context
, r
->config
, 0,
402 "AS-REQ armor missing");
403 ret
= KRB5KDC_ERR_PREAUTH_FAILED
;
407 if (fxreq
.u
.armored_data
.armor
->armor_type
!= 1) {
408 kdc_log(r
->context
, r
->config
, 0,
409 "AS-REQ armor type not ap-req");
410 ret
= KRB5KDC_ERR_PREAUTH_FAILED
;
414 ret
= krb5_decode_ap_req(r
->context
,
415 &fxreq
.u
.armored_data
.armor
->armor_value
,
418 kdc_log(r
->context
, r
->config
, 0, "AP-REQ decode failed");
422 /* Save that principal that was in the request */
423 ret
= _krb5_principalname2krb5_principal(r
->context
,
426 ap_req
.ticket
.realm
);
428 free_AP_REQ(&ap_req
);
432 ret
= _kdc_db_fetch(r
->context
, r
->config
, armor_server
,
433 HDB_F_GET_SERVER
, NULL
, NULL
, &armor_user
);
434 if(ret
== HDB_ERR_NOT_FOUND_HERE
) {
435 kdc_log(r
->context
, r
->config
, 5,
436 "armor key does not have secrets at this KDC, "
440 free_AP_REQ(&ap_req
);
441 ret
= KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN
;
445 ret
= hdb_enctype2key(r
->context
, &armor_user
->entry
, NULL
,
446 ap_req
.ticket
.enc_part
.etype
,
449 free_AP_REQ(&ap_req
);
453 ret
= krb5_verify_ap_req2(r
->context
, &ac
,
460 KRB5_KU_AP_REQ_AUTH
);
461 free_AP_REQ(&ap_req
);
465 if (ac
->remote_subkey
== NULL
) {
466 krb5_auth_con_free(r
->context
, ac
);
467 kdc_log(r
->context
, r
->config
, 0,
468 "FAST AP-REQ remote subkey missing");
469 ret
= KRB5KDC_ERR_PREAUTH_FAILED
;
473 ret
= _krb5_fast_armor_key(r
->context
,
478 krb5_auth_con_free(r
->context
, ac
);
479 krb5_free_ticket(r
->context
, ticket
);
483 krb5_free_keyblock_contents(r
->context
, &armorkey
);
485 /* verify req-checksum of the outer body */
487 ASN1_MALLOC_ENCODE(KDC_REQ_BODY
, buf
, len
, &r
->req
.req_body
, &size
, ret
);
491 ret
= KRB5KDC_ERR_PREAUTH_FAILED
;
495 ret
= krb5_verify_checksum(r
->context
, r
->armor_crypto
,
496 KRB5_KU_FAST_REQ_CHKSUM
,
498 &fxreq
.u
.armored_data
.req_checksum
);
501 kdc_log(r
->context
, r
->config
, 0,
502 "FAST request have a bad checksum");
506 ret
= krb5_decrypt_EncryptedData(r
->context
, r
->armor_crypto
,
508 &fxreq
.u
.armored_data
.enc_fast_req
,
511 kdc_log(r
->context
, r
->config
, 0,
512 "Failed to decrypt FAST request");
516 ret
= decode_KrbFastReq(data
.data
, data
.length
, &fastreq
, &size
);
518 krb5_data_free(&data
);
521 if (data
.length
!= size
) {
522 krb5_data_free(&data
);
523 ret
= KRB5KDC_ERR_PREAUTH_FAILED
;
526 krb5_data_free(&data
);
528 free_KDC_REQ_BODY(&r
->req
.req_body
);
529 ret
= copy_KDC_REQ_BODY(&fastreq
.req_body
, &r
->req
.req_body
);
533 /* check for unsupported mandatory options */
534 if (FastOptions2int(fastreq
.fast_options
) & 0xfffc) {
535 kdc_log(r
->context
, r
->config
, 0,
536 "FAST unsupported mandatory option set");
537 ret
= KRB5KDC_ERR_PREAUTH_FAILED
;
541 /* KDC MUST ignore outer pa data preauth-14 - 6.5.5 */
543 free_METHOD_DATA(r
->req
.padata
);
545 ALLOC(r
->req
.padata
);
547 ret
= copy_METHOD_DATA(&fastreq
.padata
, r
->req
.padata
);
551 free_KrbFastReq(&fastreq
);
552 free_PA_FX_FAST_REQUEST(&fxreq
);
556 krb5_free_principal(r
->context
, armor_server
);
558 _kdc_free_ent(r
->context
, armor_user
);