2 * Copyright (c) 1997-2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Portions Copyright (c) 2010 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
39 _kdc_fast_mk_response(krb5_context context
,
40 krb5_crypto armor_crypto
,
42 krb5_keyblock
*strengthen_key
,
43 KrbFastFinished
*finished
,
47 PA_FX_FAST_REPLY fxfastrep
;
48 KrbFastResponse fastrep
;
53 memset(&fxfastrep
, 0, sizeof(fxfastrep
));
54 memset(&fastrep
, 0, sizeof(fastrep
));
58 fastrep
.padata
.val
= pa_data
->val
;
59 fastrep
.padata
.len
= pa_data
->len
;
61 fastrep
.strengthen_key
= strengthen_key
;
62 fastrep
.finished
= finished
;
63 fastrep
.nonce
= nonce
;
65 ASN1_MALLOC_ENCODE(KrbFastResponse
, buf
.data
, buf
.length
,
66 &fastrep
, &size
, ret
);
69 if (buf
.length
!= size
)
70 krb5_abortx(context
, "internal asn.1 error");
72 fxfastrep
.element
= choice_PA_FX_FAST_REPLY_armored_data
;
74 ret
= krb5_encrypt_EncryptedData(context
,
80 &fxfastrep
.u
.armored_data
.enc_fast_rep
);
85 ASN1_MALLOC_ENCODE(PA_FX_FAST_REPLY
, data
->data
, data
->length
,
86 &fxfastrep
, &size
, ret
);
87 free_PA_FX_FAST_REPLY(&fxfastrep
);
90 if (data
->length
!= size
)
91 krb5_abortx(context
, "internal asn.1 error");
98 _kdc_fast_mk_error(krb5_context context
,
99 METHOD_DATA
*error_method
,
100 krb5_crypto armor_crypto
,
101 const KDC_REQ_BODY
*req_body
,
102 krb5_error_code outer_error
,
104 krb5_principal error_client
,
105 krb5_principal error_server
,
106 time_t *csec
, int *cusec
,
107 krb5_data
*error_msg
)
113 krb5_data_zero(&e_data
);
116 PA_FX_FAST_REPLY fxfastrep
;
117 KrbFastResponse fastrep
;
119 memset(&fxfastrep
, 0, sizeof(fxfastrep
));
120 memset(&fastrep
, 0, sizeof(fastrep
));
122 /* first add the KRB-ERROR to the fast errors */
124 ret
= krb5_mk_error(context
,
136 ret
= krb5_padata_add(context
, error_method
,
137 KRB5_PADATA_FX_ERROR
,
138 e_data
.data
, e_data
.length
);
140 krb5_data_free(&e_data
);
144 if (/* hide_principal */ 0) {
150 ret
= krb5_padata_add(context
, error_method
,
151 KRB5_PADATA_FX_COOKIE
,
156 ret
= _kdc_fast_mk_response(context
, armor_crypto
,
157 error_method
, NULL
, NULL
,
158 req_body
->nonce
, &e_data
);
159 free_METHOD_DATA(error_method
);
163 ret
= krb5_padata_add(context
, error_method
,
165 e_data
.data
, e_data
.length
);
173 if (error_method
&& error_method
->len
) {
174 ASN1_MALLOC_ENCODE(METHOD_DATA
, e_data
.data
, e_data
.length
,
175 error_method
, &size
, ret
);
178 if (e_data
.length
!= size
)
179 krb5_abortx(context
, "internal asn.1 error");
182 ret
= krb5_mk_error(context
,
185 (e_data
.length
? &e_data
: NULL
),
191 krb5_data_free(&e_data
);
198 _kdc_fast_unwrap_request(kdc_request_t r
)
200 krb5_principal armor_server
= NULL
;
201 hdb_entry_ex
*armor_user
= NULL
;
202 PA_FX_FAST_REQUEST fxreq
;
203 krb5_auth_context ac
= NULL
;
204 krb5_ticket
*ticket
= NULL
;
205 krb5_flags ap_req_options
;
206 Key
*armor_key
= NULL
;
207 krb5_keyblock armorkey
;
217 pa
= _kdc_find_padata(&r
->req
, &i
, KRB5_PADATA_FX_FAST
);
221 ret
= decode_PA_FX_FAST_REQUEST(pa
->padata_value
.data
,
222 pa
->padata_value
.length
,
227 if (len
!= pa
->padata_value
.length
) {
228 ret
= KRB5KDC_ERR_PREAUTH_FAILED
;
232 if (fxreq
.element
!= choice_PA_FX_FAST_REQUEST_armored_data
) {
233 kdc_log(r
->context
, r
->config
, 0,
234 "AS-REQ FAST contain unknown type: %d", (int)fxreq
.element
);
235 ret
= KRB5KDC_ERR_PREAUTH_FAILED
;
239 /* pull out armor key */
240 if (fxreq
.u
.armored_data
.armor
== NULL
) {
241 kdc_log(r
->context
, r
->config
, 0,
242 "AS-REQ armor missing");
243 ret
= KRB5KDC_ERR_PREAUTH_FAILED
;
247 if (fxreq
.u
.armored_data
.armor
->armor_type
!= 1) {
248 kdc_log(r
->context
, r
->config
, 0,
249 "AS-REQ armor type not ap-req");
250 ret
= KRB5KDC_ERR_PREAUTH_FAILED
;
254 ret
= krb5_decode_ap_req(r
->context
,
255 &fxreq
.u
.armored_data
.armor
->armor_value
,
258 kdc_log(r
->context
, r
->config
, 0, "AP-REQ decode failed");
262 /* Save that principal that was in the request */
263 ret
= _krb5_principalname2krb5_principal(r
->context
,
266 ap_req
.ticket
.realm
);
268 free_AP_REQ(&ap_req
);
272 ret
= _kdc_db_fetch(r
->context
, r
->config
, armor_server
,
273 HDB_F_GET_SERVER
, NULL
, NULL
, &armor_user
);
274 if(ret
== HDB_ERR_NOT_FOUND_HERE
) {
275 kdc_log(r
->context
, r
->config
, 5,
276 "armor key does not have secrets at this KDC, "
278 ret
= KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN
;
281 free_AP_REQ(&ap_req
);
282 ret
= KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN
;
286 ret
= hdb_enctype2key(r
->context
, &armor_user
->entry
,
287 ap_req
.ticket
.enc_part
.etype
,
290 free_AP_REQ(&ap_req
);
294 ret
= krb5_verify_ap_req2(r
->context
, &ac
,
301 KRB5_KU_AP_REQ_AUTH
);
302 free_AP_REQ(&ap_req
);
306 if (ac
->remote_subkey
== NULL
) {
307 krb5_auth_con_free(r
->context
, ac
);
308 kdc_log(r
->context
, r
->config
, 0,
309 "FAST AP-REQ remote subkey missing");
310 ret
= KRB5KDC_ERR_PREAUTH_FAILED
;
314 ret
= _krb5_fast_armor_key(r
->context
,
319 krb5_auth_con_free(r
->context
, ac
);
320 krb5_free_ticket(r
->context
, ticket
);
324 krb5_free_keyblock_contents(r
->context
, &armorkey
);
326 /* verify req-checksum of the outer body */
328 ASN1_MALLOC_ENCODE(KDC_REQ_BODY
, buf
, len
, &r
->req
.req_body
, &size
, ret
);
332 ret
= KRB5KDC_ERR_PREAUTH_FAILED
;
336 ret
= krb5_verify_checksum(r
->context
, r
->armor_crypto
,
337 KRB5_KU_FAST_REQ_CHKSUM
,
339 &fxreq
.u
.armored_data
.req_checksum
);
342 kdc_log(r
->context
, r
->config
, 0,
343 "FAST request have a bad checksum");
347 ret
= krb5_decrypt_EncryptedData(r
->context
, r
->armor_crypto
,
349 &fxreq
.u
.armored_data
.enc_fast_req
,
352 kdc_log(r
->context
, r
->config
, 0,
353 "Failed to decrypt FAST request");
357 ret
= decode_KrbFastReq(data
.data
, data
.length
, &fastreq
, &size
);
359 krb5_data_free(&data
);
362 if (data
.length
!= size
) {
363 krb5_data_free(&data
);
364 ret
= KRB5KDC_ERR_PREAUTH_FAILED
;
367 krb5_data_free(&data
);
369 free_KDC_REQ_BODY(&r
->req
.req_body
);
370 ret
= copy_KDC_REQ_BODY(&fastreq
.req_body
, &r
->req
.req_body
);
374 /* check for unsupported mandatory options */
375 if (FastOptions2int(fastreq
.fast_options
) & 0xfffc) {
376 kdc_log(r
->context
, r
->config
, 0,
377 "FAST unsupported mandatory option set");
378 ret
= KRB5KDC_ERR_PREAUTH_FAILED
;
382 /* KDC MUST ignore outer pa data preauth-14 - 6.5.5 */
384 free_METHOD_DATA(r
->req
.padata
);
386 ALLOC(r
->req
.padata
);
388 ret
= copy_METHOD_DATA(&fastreq
.padata
, r
->req
.padata
);
392 free_KrbFastReq(&fastreq
);
393 free_PA_FX_FAST_REQUEST(&fxreq
);
397 krb5_free_principal(r
->context
, armor_server
);
399 _kdc_free_ent(r
->context
, armor_user
);