hx509: For times before 2050 use UTCTime (fix pasto)
[heimdal.git] / kdc / fast.c
blob64b0f507ad882b172742e9c2409a761ad19de60d
1 /*
2 * Copyright (c) 1997-2011 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
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
10 * are met:
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
33 * SUCH DAMAGE.
36 #include "kdc_locl.h"
38 static krb5_error_code
39 get_fastuser_crypto(astgs_request_t r, krb5_enctype enctype,
40 krb5_crypto *crypto)
42 krb5_principal fast_princ;
43 hdb_entry_ex *fast_user = NULL;
44 Key *cookie_key = NULL;
45 krb5_error_code ret;
47 *crypto = NULL;
49 ret = krb5_make_principal(r->context, &fast_princ,
50 KRB5_WELLKNOWN_ORG_H5L_REALM,
51 KRB5_WELLKNOWN_NAME, "org.h5l.fast-cookie", NULL);
52 if (ret)
53 goto out;
55 ret = _kdc_db_fetch(r->context, r->config, fast_princ,
56 HDB_F_GET_FAST_COOKIE, NULL, NULL, &fast_user);
57 krb5_free_principal(r->context, fast_princ);
58 if (ret)
59 goto out;
61 if (enctype == KRB5_ENCTYPE_NULL)
62 ret = _kdc_get_preferred_key(r->context, r->config, fast_user,
63 "fast-cookie", &enctype, &cookie_key);
64 else
65 ret = hdb_enctype2key(r->context, &fast_user->entry, NULL,
66 enctype, &cookie_key);
67 if (ret)
68 goto out;
70 ret = krb5_crypto_init(r->context, &cookie_key->key, 0, crypto);
71 if (ret)
72 goto out;
74 out:
75 if (fast_user)
76 _kdc_free_ent(r->context, fast_user);
78 return ret;
82 static krb5_error_code
83 fast_parse_cookie(astgs_request_t r, const PA_DATA *pa)
85 krb5_crypto crypto = NULL;
86 krb5_error_code ret;
87 KDCFastCookie data;
88 krb5_data d1;
89 size_t len;
91 ret = decode_KDCFastCookie(pa->padata_value.data,
92 pa->padata_value.length,
93 &data, &len);
94 if (ret)
95 return ret;
97 if (len != pa->padata_value.length || strcmp("H5L1", data.version) != 0) {
98 free_KDCFastCookie(&data);
99 return KRB5KDC_ERR_POLICY;
102 ret = get_fastuser_crypto(r, data.cookie.etype, &crypto);
103 if (ret)
104 goto out;
106 ret = krb5_decrypt_EncryptedData(r->context, crypto,
107 KRB5_KU_H5L_COOKIE,
108 &data.cookie, &d1);
109 krb5_crypto_destroy(r->context, crypto);
110 if (ret)
111 goto out;
113 ret = decode_KDCFastState(d1.data, d1.length, &r->fast, &len);
114 krb5_data_free(&d1);
115 if (ret)
116 goto out;
118 if (r->fast.expiration < kdc_time) {
119 kdc_log(r->context, r->config, 2, "fast cookie expired");
120 ret = KRB5KDC_ERR_POLICY;
121 goto out;
124 out:
125 free_KDCFastCookie(&data);
127 return ret;
130 static krb5_error_code
131 fast_add_cookie(astgs_request_t r, METHOD_DATA *method_data)
133 krb5_crypto crypto = NULL;
134 KDCFastCookie shell;
135 krb5_error_code ret;
136 krb5_data data;
137 size_t size;
139 memset(&shell, 0, sizeof(shell));
141 r->fast.expiration = kdc_time + FAST_EXPIRATION_TIME;
143 ASN1_MALLOC_ENCODE(KDCFastState, data.data, data.length,
144 &r->fast, &size, ret);
145 if (ret)
146 return ret;
147 heim_assert(size == data.length, "internal asn1 encoder error");
149 ret = get_fastuser_crypto(r, KRB5_ENCTYPE_NULL, &crypto);
150 if (ret)
151 goto out;
153 ret = krb5_encrypt_EncryptedData(r->context, crypto,
154 KRB5_KU_H5L_COOKIE,
155 data.data, data.length, 0,
156 &shell.cookie);
157 krb5_crypto_destroy(r->context, crypto);
158 if (ret)
159 goto out;
161 free(data.data);
163 shell.version = "H5L1";
165 ASN1_MALLOC_ENCODE(KDCFastCookie, data.data, data.length,
166 &shell, &size, ret);
167 free_EncryptedData(&shell.cookie);
168 if (ret)
169 goto out;
170 heim_assert(size == data.length, "internal asn1 encoder error");
172 ret = krb5_padata_add(r->context, method_data,
173 KRB5_PADATA_FX_COOKIE,
174 data.data, data.length);
175 out:
176 if (ret)
177 free(data.data);
178 return ret;
181 krb5_error_code
182 _kdc_fast_mk_response(krb5_context context,
183 krb5_crypto armor_crypto,
184 METHOD_DATA *pa_data,
185 krb5_keyblock *strengthen_key,
186 KrbFastFinished *finished,
187 krb5uint32 nonce,
188 krb5_data *data)
190 PA_FX_FAST_REPLY fxfastrep;
191 KrbFastResponse fastrep;
192 krb5_error_code ret;
193 krb5_data buf;
194 size_t size;
196 memset(&fxfastrep, 0, sizeof(fxfastrep));
197 memset(&fastrep, 0, sizeof(fastrep));
198 krb5_data_zero(data);
200 if (pa_data) {
201 fastrep.padata.val = pa_data->val;
202 fastrep.padata.len = pa_data->len;
204 fastrep.strengthen_key = strengthen_key;
205 fastrep.finished = finished;
206 fastrep.nonce = nonce;
208 ASN1_MALLOC_ENCODE(KrbFastResponse, buf.data, buf.length,
209 &fastrep, &size, ret);
210 if (ret)
211 return ret;
212 if (buf.length != size)
213 krb5_abortx(context, "internal asn.1 error");
215 fxfastrep.element = choice_PA_FX_FAST_REPLY_armored_data;
217 ret = krb5_encrypt_EncryptedData(context,
218 armor_crypto,
219 KRB5_KU_FAST_REP,
220 buf.data,
221 buf.length,
223 &fxfastrep.u.armored_data.enc_fast_rep);
224 krb5_data_free(&buf);
225 if (ret)
226 return ret;
228 ASN1_MALLOC_ENCODE(PA_FX_FAST_REPLY, data->data, data->length,
229 &fxfastrep, &size, ret);
230 free_PA_FX_FAST_REPLY(&fxfastrep);
231 if (ret)
232 return ret;
233 if (data->length != size)
234 krb5_abortx(context, "internal asn.1 error");
236 return 0;
240 krb5_error_code
241 _kdc_fast_mk_error(astgs_request_t r,
242 METHOD_DATA *error_method,
243 krb5_crypto armor_crypto,
244 const KDC_REQ_BODY *req_body,
245 krb5_error_code outer_error,
246 const char *e_text,
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)
253 krb5_context context = r->context;
254 krb5_error_code ret;
255 krb5_data e_data;
256 size_t size;
258 krb5_data_zero(&e_data);
260 if (armor_crypto || (r && r->fast.fast_state.len)) {
261 if (r)
262 ret = fast_add_cookie(r, error_method);
263 else
264 ret = krb5_padata_add(context, error_method,
265 KRB5_PADATA_FX_COOKIE,
266 NULL, 0);
267 if (ret) {
268 kdc_log(r->context, r->config, 1, "failed to add fast cookie with: %d", ret);
269 free_METHOD_DATA(error_method);
270 return ret;
274 if (armor_crypto) {
275 PA_FX_FAST_REPLY fxfastrep;
276 KrbFastResponse fastrep;
278 memset(&fxfastrep, 0, sizeof(fxfastrep));
279 memset(&fastrep, 0, sizeof(fastrep));
281 /* first add the KRB-ERROR to the fast errors */
283 ret = krb5_mk_error_ext(context,
284 outer_error,
285 e_text,
286 NULL,
287 error_server,
288 error_client_name,
289 error_client_realm,
290 NULL,
291 NULL,
292 &e_data);
293 if (ret)
294 return ret;
296 ret = krb5_padata_add(context, error_method,
297 KRB5_PADATA_FX_ERROR,
298 e_data.data, e_data.length);
299 if (ret) {
300 krb5_data_free(&e_data);
301 return ret;
304 error_client_name = NULL;
305 error_client_realm = NULL;
306 error_server = NULL;
307 e_text = NULL;
309 ret = _kdc_fast_mk_response(context, armor_crypto,
310 error_method, NULL, NULL,
311 req_body->nonce, &e_data);
312 free_METHOD_DATA(error_method);
313 if (ret)
314 return ret;
316 ret = krb5_padata_add(context, error_method,
317 KRB5_PADATA_FX_FAST,
318 e_data.data, e_data.length);
319 if (ret)
320 return ret;
323 if (error_method && error_method->len) {
324 ASN1_MALLOC_ENCODE(METHOD_DATA, e_data.data, e_data.length,
325 error_method, &size, ret);
326 if (ret)
327 return ret;
328 if (e_data.length != size)
329 krb5_abortx(context, "internal asn.1 error");
332 ret = krb5_mk_error_ext(context,
333 outer_error,
334 e_text,
335 (e_data.length ? &e_data : NULL),
336 error_server,
337 error_client_name,
338 error_client_realm,
339 csec,
340 cusec,
341 error_msg);
342 krb5_data_free(&e_data);
344 return ret;
347 static krb5_error_code
348 fast_unwrap_request(astgs_request_t r)
350 krb5_principal armor_server = NULL;
351 hdb_entry_ex *armor_user = NULL;
352 PA_FX_FAST_REQUEST fxreq;
353 krb5_auth_context ac = NULL;
354 krb5_ticket *ticket = NULL;
355 krb5_flags ap_req_options;
356 Key *armor_key = NULL;
357 krb5_keyblock armorkey;
358 krb5_error_code ret;
359 krb5_ap_req ap_req;
360 unsigned char *buf = NULL;
361 KrbFastReq fastreq;
362 size_t len, size;
363 krb5_data data;
364 const PA_DATA *pa;
365 int i = 0;
367 pa = _kdc_find_padata(&r->req, &i, KRB5_PADATA_FX_FAST);
368 if (pa == NULL)
369 return 0;
371 ret = decode_PA_FX_FAST_REQUEST(pa->padata_value.data,
372 pa->padata_value.length,
373 &fxreq,
374 &len);
375 if (ret)
376 goto out;
377 if (len != pa->padata_value.length) {
378 ret = KRB5KDC_ERR_PREAUTH_FAILED;
379 goto out;
382 if (fxreq.element != choice_PA_FX_FAST_REQUEST_armored_data) {
383 kdc_log(r->context, r->config, 2,
384 "AS-REQ FAST contain unknown type: %d", (int)fxreq.element);
385 ret = KRB5KDC_ERR_PREAUTH_FAILED;
386 goto out;
389 /* pull out armor key */
390 if (fxreq.u.armored_data.armor == NULL) {
391 kdc_log(r->context, r->config, 2,
392 "AS-REQ armor missing");
393 ret = KRB5KDC_ERR_PREAUTH_FAILED;
394 goto out;
397 if (fxreq.u.armored_data.armor->armor_type != 1) {
398 kdc_log(r->context, r->config, 2,
399 "AS-REQ armor type not ap-req");
400 ret = KRB5KDC_ERR_PREAUTH_FAILED;
401 goto out;
404 ret = krb5_decode_ap_req(r->context,
405 &fxreq.u.armored_data.armor->armor_value,
406 &ap_req);
407 if(ret) {
408 kdc_log(r->context, r->config, 2, "AP-REQ decode failed");
409 goto out;
412 /* Save that principal that was in the request */
413 ret = _krb5_principalname2krb5_principal(r->context,
414 &armor_server,
415 ap_req.ticket.sname,
416 ap_req.ticket.realm);
417 if (ret) {
418 free_AP_REQ(&ap_req);
419 goto out;
422 ret = _kdc_db_fetch(r->context, r->config, armor_server,
423 HDB_F_GET_KRBTGT
424 | HDB_F_DELAY_NEW_KEYS,
425 NULL, NULL, &armor_user);
426 if(ret == HDB_ERR_NOT_FOUND_HERE) {
427 kdc_log(r->context, r->config, 5,
428 "armor key does not have secrets at this KDC, "
429 "need to proxy");
430 free_AP_REQ(&ap_req);
431 goto out;
432 } else if (ret) {
433 free_AP_REQ(&ap_req);
434 ret = KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN;
435 goto out;
438 ret = hdb_enctype2key(r->context, &armor_user->entry, NULL,
439 ap_req.ticket.enc_part.etype,
440 &armor_key);
441 if (ret) {
442 free_AP_REQ(&ap_req);
443 goto out;
446 ret = krb5_verify_ap_req2(r->context, &ac,
447 &ap_req,
448 armor_server,
449 &armor_key->key,
451 &ap_req_options,
452 &ticket,
453 KRB5_KU_AP_REQ_AUTH);
454 free_AP_REQ(&ap_req);
455 if (ret)
456 goto out;
458 if (ac->remote_subkey == NULL) {
459 krb5_auth_con_free(r->context, ac);
460 kdc_log(r->context, r->config, 2,
461 "FAST AP-REQ remote subkey missing");
462 ret = KRB5KDC_ERR_PREAUTH_FAILED;
463 goto out;
466 ret = _krb5_fast_armor_key(r->context,
467 ac->remote_subkey,
468 &ticket->ticket.key,
469 &armorkey,
470 &r->armor_crypto);
471 krb5_auth_con_free(r->context, ac);
472 krb5_free_ticket(r->context, ticket);
473 if (ret)
474 goto out;
476 krb5_free_keyblock_contents(r->context, &armorkey);
478 /* verify req-checksum of the outer body */
480 ASN1_MALLOC_ENCODE(KDC_REQ_BODY, buf, len, &r->req.req_body, &size, ret);
481 if (ret)
482 goto out;
483 if (size != len) {
484 ret = KRB5KDC_ERR_PREAUTH_FAILED;
485 goto out;
488 ret = krb5_verify_checksum(r->context, r->armor_crypto,
489 KRB5_KU_FAST_REQ_CHKSUM,
490 buf, len,
491 &fxreq.u.armored_data.req_checksum);
492 if (ret) {
493 kdc_log(r->context, r->config, 2,
494 "FAST request have a bad checksum");
495 goto out;
498 ret = krb5_decrypt_EncryptedData(r->context, r->armor_crypto,
499 KRB5_KU_FAST_ENC,
500 &fxreq.u.armored_data.enc_fast_req,
501 &data);
502 if (ret) {
503 kdc_log(r->context, r->config, 2,
504 "Failed to decrypt FAST request");
505 goto out;
508 ret = decode_KrbFastReq(data.data, data.length, &fastreq, &size);
509 if (ret) {
510 krb5_data_free(&data);
511 goto out;
513 if (data.length != size) {
514 krb5_data_free(&data);
515 ret = KRB5KDC_ERR_PREAUTH_FAILED;
516 goto out;
518 krb5_data_free(&data);
520 free_KDC_REQ_BODY(&r->req.req_body);
521 ret = copy_KDC_REQ_BODY(&fastreq.req_body, &r->req.req_body);
522 if (ret)
523 goto out;
525 /* check for unsupported mandatory options */
526 if (FastOptions2int(fastreq.fast_options) & 0xfffc) {
527 kdc_log(r->context, r->config, 2,
528 "FAST unsupported mandatory option set");
529 ret = KRB5KDC_ERR_PREAUTH_FAILED;
530 goto out;
533 /* KDC MUST ignore outer pa data preauth-14 - 6.5.5 */
534 if (r->req.padata)
535 free_METHOD_DATA(r->req.padata);
536 else
537 ALLOC(r->req.padata);
539 ret = copy_METHOD_DATA(&fastreq.padata, r->req.padata);
540 if (ret)
541 goto out;
543 free_KrbFastReq(&fastreq);
544 free_PA_FX_FAST_REQUEST(&fxreq);
546 out:
547 if (armor_server)
548 krb5_free_principal(r->context, armor_server);
549 if(armor_user)
550 _kdc_free_ent(r->context, armor_user);
551 free(buf);
553 return ret;
556 krb5_error_code
557 _kdc_fast_unwrap_request(astgs_request_t r)
559 krb5_error_code ret;
560 const PA_DATA *pa;
561 int i = 0;
563 ret = fast_unwrap_request(r);
564 if (ret)
565 return ret;
568 * Non-FAST mechanisms may use FX-COOKIE to manage state.
570 pa = _kdc_find_padata(&r->req, &i, KRB5_PADATA_FX_COOKIE);
571 if (pa) {
572 ret = fast_parse_cookie(r, pa);
573 if (ret)
574 return ret;
577 return 0;
580 void
581 _kdc_free_fast_state(KDCFastState *state)
583 size_t i;
585 for (i = 0; i < state->fast_state.len; i++) {
586 PA_DATA *pa = &state->fast_state.val[i];
588 if (pa->padata_value.data)
589 memset_s(pa->padata_value.data, 0,
590 pa->padata_value.length, pa->padata_value.length);
592 free_KDCFastState(state);