fast negotiation projec
[krb5.git] / src / lib / krb5 / krb / fast.c
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  * lib/krb5/krb/fast.c
4  *
5  * Copyright (C) 2009 by the Massachusetts Institute of Technology.
6  * All rights reserved.
7  *
8  * Export of this software from the United States of America may
9  *   require a specific license from the United States Government.
10  *   It is the responsibility of any person or organization contemplating
11  *   export to obtain such a license before exporting.
12  *
13  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
14  * distribute this software and its documentation for any purpose and
15  * without fee is hereby granted, provided that the above copyright
16  * notice appear in all copies and that both that copyright notice and
17  * this permission notice appear in supporting documentation, and that
18  * the name of M.I.T. not be used in advertising or publicity pertaining
19  * to distribution of the software without specific, written prior
20  * permission.  Furthermore if you modify this software you must label
21  * your software as modified software and not distribute it in such a
22  * fashion that it might be confused with the original M.I.T. software.
23  * M.I.T. makes no representations about the suitability of
24  * this software for any purpose.  It is provided "as is" without express
25  * or implied warranty.
26  *
27  *
28  *
29  */
30
31 #include <k5-int.h>
32
33 /*
34  * It is possible to support sending a request that includes both a
35  * FAST and normal version.  This would complicate the
36  * pre-authentication logic significantly.  You would need to maintain
37  * two contexts, one for FAST and one for normal use.  In adition, you
38  * would need to manage the security issues surrounding downgrades.
39  * However trying FAST at all requires an armor key.  Generally in
40  * obtaining the armor key, the client learns enough to know that FAST
41  * is supported.  If not, the client can see FAST in the
42  * preauth_required error's padata and retry with FAST.  So, this
43  * implementation does not support FAST+normal.
44  *
45  * We store the outer version of the request to use .  The caller
46  * stores the inner version.  We handle the encoding of the request
47  * body (and request) and provide encoded request bodies for the
48  * caller to use as these may be used for checksums.  In the AS case
49  * we also evaluate whether to continue a conversation as one of the
50  * important questions there is the presence of a cookie.
51  */
52 #include "fast.h"
53 #include "int-proto.h"
54
55
56 static krb5_error_code
57 fast_armor_ap_request(krb5_context context,
58                       struct krb5int_fast_request_state *state,
59                       krb5_ccache ccache, krb5_principal target_principal)
60 {
61     krb5_error_code retval = 0;
62     krb5_creds creds, *out_creds = NULL;
63     krb5_auth_context authcontext = NULL;
64     krb5_data encoded_authenticator;
65     krb5_fast_armor *armor = NULL;
66     krb5_keyblock *subkey = NULL, *armor_key = NULL;
67     encoded_authenticator.data = NULL;
68     memset(&creds, 0, sizeof(creds));
69     creds.server = target_principal;
70     retval = krb5_cc_get_principal(context, ccache, &creds.client);
71     if (retval == 0)
72         retval = krb5_get_credentials(context, 0, ccache,  &creds, &out_creds);
73     if (retval == 0)
74         retval = krb5_mk_req_extended(context, &authcontext, AP_OPTS_USE_SUBKEY, NULL /*data*/,
75                                       out_creds, &encoded_authenticator);
76     if (retval == 0)
77         retval = krb5_auth_con_getsendsubkey(context, authcontext, &subkey);
78     if (retval == 0)
79         retval = krb5_c_fx_cf2_simple(context, subkey, "subkeyarmor",
80                                       &out_creds->keyblock, "ticketarmor", &armor_key);
81     if (retval == 0) {
82         armor = calloc(1, sizeof(krb5_fast_armor));
83         if (armor == NULL)
84             retval = ENOMEM;
85     }
86     if (retval == 0) {
87         armor->armor_type = KRB5_FAST_ARMOR_AP_REQUEST;
88         armor->armor_value = encoded_authenticator;
89         encoded_authenticator.data = NULL;
90         encoded_authenticator.length = 0;
91         state->armor = armor;
92         armor = NULL;
93         state->armor_key = armor_key;
94         armor_key = NULL;
95     }
96     krb5_free_keyblock(context, armor_key);
97     krb5_free_keyblock(context, subkey);
98     if (out_creds)
99         krb5_free_creds(context, out_creds);
100     /* target_principal is owned by caller. */
101     creds.server = NULL;
102     krb5_free_cred_contents(context, &creds);
103     if (encoded_authenticator.data)
104         krb5_free_data_contents(context, &encoded_authenticator);
105     krb5_auth_con_free(context, authcontext);
106     return retval;
107 }
108
109 krb5_error_code
110 krb5int_fast_prep_req_body(krb5_context context, struct krb5int_fast_request_state *state,
111                            krb5_kdc_req *request, krb5_data **encoded_request_body)
112 {
113     krb5_error_code retval = 0;
114     krb5_data *local_encoded_request_body = NULL;
115     assert(state != NULL);
116     *encoded_request_body = NULL;
117     if (state->armor_key == NULL) {
118         return   encode_krb5_kdc_req_body(request, encoded_request_body);
119     }
120     state->fast_outer_request = *request;
121     state->fast_outer_request.padata = NULL;
122     if (retval == 0)
123         retval = encode_krb5_kdc_req_body(&state->fast_outer_request,
124                                           &local_encoded_request_body);
125     if (retval == 0) {
126         *encoded_request_body = local_encoded_request_body;
127         local_encoded_request_body = NULL;
128     }
129     if (local_encoded_request_body != NULL)
130         krb5_free_data(context, local_encoded_request_body);
131     return retval;
132 }
133
134 krb5_error_code
135 krb5int_fast_as_armor(krb5_context context,
136                       struct krb5int_fast_request_state *state,
137                       krb5_gic_opt_ext *opte,
138                       krb5_kdc_req *request)
139 {
140     krb5_error_code retval = 0;
141     krb5_ccache ccache = NULL;
142     krb5_principal target_principal = NULL;
143     krb5_data *target_realm;
144     krb5_clear_error_message(context);
145     target_realm = krb5_princ_realm(context, request->server);
146     if (opte->opt_private->fast_ccache_name) {
147         state->fast_state_flags |= KRB5INT_FAST_ARMOR_AVAIL;
148         retval = krb5_cc_resolve(context, opte->opt_private->fast_ccache_name,
149                                  &ccache);
150         if (retval == 0) {
151             retval = krb5_tgtname(context, target_realm, target_realm,
152                                   &target_principal);
153         }
154         if (retval == 0) {
155             krb5_data config_data;
156             config_data.data = NULL;
157             retval = krb5_cc_get_config(context, ccache, target_principal,
158                                         KRB5_CONF_FAST_AVAIL, &config_data);
159             if ((retval == 0) && config_data.data )
160                 state->fast_state_flags |= KRB5INT_FAST_DO_FAST;
161             krb5_free_data_contents(context, &config_data);
162             retval = 0;
163         }
164         if (opte->opt_private->fast_flags& KRB5_FAST_REQUIRED)
165             state->fast_state_flags |= KRB5INT_FAST_DO_FAST;
166         if (retval == 0 && (state->fast_state_flags & KRB5INT_FAST_DO_FAST)) {
167             retval = fast_armor_ap_request(context, state, ccache,
168                                            target_principal);
169         }
170         if (retval != 0) {
171             const char * errmsg;
172             errmsg = krb5_get_error_message(context, retval);
173             if (errmsg) {
174                 krb5_set_error_message(context, retval, "%s constructing AP-REQ armor", errmsg);
175                 krb5_free_error_message(context, errmsg);
176             }
177         }
178     }
179     if (ccache)
180         krb5_cc_close(context, ccache);
181     if (target_principal)
182         krb5_free_principal(context, target_principal);
183     return retval;
184 }
185
186
187 krb5_error_code
188 krb5int_fast_prep_req(krb5_context context,
189                       struct krb5int_fast_request_state *state,
190                       krb5_kdc_req *request,
191                       const krb5_data *to_be_checksummed,
192                       kdc_req_encoder_proc encoder,
193                       krb5_data **encoded_request)
194 {
195     krb5_error_code retval = 0;
196     krb5_pa_data *pa_array[2];
197     krb5_pa_data pa[2];
198     krb5_fast_req fast_req;
199     krb5_fast_armored_req *armored_req = NULL;
200     krb5_data *encoded_fast_req = NULL;
201     krb5_data *encoded_armored_req = NULL;
202     krb5_data *local_encoded_result = NULL;
203     krb5_cksumtype cksumtype;
204     krb5_data random_data;
205     char random_buf[4];
206
207
208     assert(state != NULL);
209     assert(state->fast_outer_request.padata == NULL);
210     memset(pa_array, 0, sizeof pa_array);
211     if (state->armor_key == NULL) {
212         return encoder(request, encoded_request);
213     }
214 /* Fill in a fresh random nonce for each inner request*/
215     random_data.length = 4;
216     random_data.data = (char *)random_buf;
217     retval = krb5_c_random_make_octets(context, &random_data);
218     if (retval == 0) {
219         request->nonce = 0x7fffffff & load_32_n(random_buf);
220         state->nonce = request->nonce;
221     }
222     fast_req.req_body =  request;
223     if (fast_req.req_body->padata == NULL) {
224         fast_req.req_body->padata = calloc(1, sizeof(krb5_pa_data *));
225         if (fast_req.req_body->padata == NULL)
226             retval = ENOMEM;
227     }
228     fast_req.fast_options = state->fast_options;
229     if (retval == 0)
230         retval = encode_krb5_fast_req(&fast_req, &encoded_fast_req);
231     if (retval == 0) {
232         armored_req = calloc(1, sizeof(krb5_fast_armored_req));
233         if (armored_req == NULL)
234             retval = ENOMEM;
235     }
236     if (retval == 0)
237         armored_req->armor = state->armor;
238     if (retval == 0)
239         retval = krb5int_c_mandatory_cksumtype(context, state->armor_key->enctype,
240                                                &cksumtype);
241     if (retval ==0)
242         retval = krb5_c_make_checksum(context, cksumtype, state->armor_key,
243                                       KRB5_KEYUSAGE_FAST_REQ_CHKSUM, to_be_checksummed,
244                                       &armored_req->req_checksum);
245     if (retval == 0)
246         retval = krb5_encrypt_helper(context, state->armor_key,
247                                      KRB5_KEYUSAGE_FAST_ENC, encoded_fast_req,
248                                      &armored_req->enc_part);
249     if (retval == 0)
250         retval = encode_krb5_pa_fx_fast_request(armored_req, &encoded_armored_req);
251     if (retval==0) {
252         pa[0].pa_type = KRB5_PADATA_FX_FAST;
253         pa[0].contents = (unsigned char *) encoded_armored_req->data;
254         pa[0].length = encoded_armored_req->length;
255         pa_array[0] = &pa[0];
256     }
257     state->fast_outer_request.padata = pa_array;
258     if(retval == 0)
259         retval = encoder(&state->fast_outer_request, &local_encoded_result);
260     if (retval == 0) {
261         *encoded_request = local_encoded_result;
262         local_encoded_result = NULL;
263     }
264     if (encoded_armored_req)
265         krb5_free_data(context, encoded_armored_req);
266     if (armored_req) {
267         armored_req->armor = NULL; /*owned by state*/
268         krb5_free_fast_armored_req(context, armored_req);
269     }
270     if (encoded_fast_req)
271         krb5_free_data(context, encoded_fast_req);
272     if (local_encoded_result)
273         krb5_free_data(context, local_encoded_result);
274     state->fast_outer_request.padata = NULL;
275     return retval;
276 }
277
278 static krb5_error_code
279 decrypt_fast_reply(krb5_context context,
280                    struct krb5int_fast_request_state *state,
281                    krb5_pa_data **in_padata,
282                    krb5_fast_response **response)
283 {
284     krb5_error_code retval = 0;
285     krb5_data scratch;
286     krb5_enc_data *encrypted_response = NULL;
287     krb5_pa_data *fx_reply = NULL;
288     krb5_fast_response *local_resp = NULL;
289     assert(state != NULL);
290     assert(state->armor_key);
291     fx_reply = krb5int_find_pa_data(context, in_padata, KRB5_PADATA_FX_FAST);
292     if (fx_reply == NULL)
293         retval = KRB5_ERR_FAST_REQUIRED;
294     if (retval == 0) {
295         scratch.data = (char *) fx_reply->contents;
296         scratch.length = fx_reply->length;
297         retval = decode_krb5_pa_fx_fast_reply(&scratch, &encrypted_response);
298     }
299     scratch.data = NULL;
300     if (retval == 0) {
301         scratch.data = malloc(encrypted_response->ciphertext.length);
302         if (scratch.data == NULL)
303             retval = ENOMEM;
304         scratch.length = encrypted_response->ciphertext.length;
305     }
306     if (retval == 0)
307         retval = krb5_c_decrypt(context, state->armor_key,
308                                 KRB5_KEYUSAGE_FAST_REP, NULL,
309                                 encrypted_response, &scratch);
310     if (retval != 0) {
311         const char * errmsg;
312         errmsg = krb5_get_error_message(context, retval);
313         krb5_set_error_message(context, retval, "%s while decrypting FAST reply", errmsg);
314         krb5_free_error_message(context, errmsg);
315     }
316     if (retval == 0)
317         retval = decode_krb5_fast_response(&scratch, &local_resp);
318     if (retval == 0) {
319         if (local_resp->nonce != state->nonce) {
320             retval = KRB5_KDCREP_MODIFIED;
321             krb5_set_error_message(context, retval, "nonce modified in FAST response: KDC response modified");
322         }
323     }
324     if (retval == 0) {
325         *response = local_resp;
326         local_resp = NULL;
327     }
328     if (scratch.data)
329         free(scratch.data);
330     if (encrypted_response)
331         krb5_free_enc_data(context, encrypted_response);
332     if (local_resp)
333         krb5_free_fast_response(context, local_resp);
334     return retval;
335 }
336
337 /*
338  * FAST separates two concepts: the set of padata we're using to
339  * decide what pre-auth mechanisms to use and the set of padata we're
340  * making available to mechanisms in order for them to respond to an
341  * error.  The plugin interface in March 2009 does not permit
342  * separating these concepts for the plugins.  This function makes
343  * both available for future revisions to the plugin interface.  It
344  * also re-encodes the padata from the current error as a encoded
345  * typed-data and puts that in the e_data field.  That will allow
346  * existing plugins with the old interface to find the error data.
347  * The output parameter out_padata contains the padata from the error
348  * whenever padata  is available (all the time with fast).
349  */
350 krb5_error_code
351 krb5int_fast_process_error(krb5_context context,
352                            struct krb5int_fast_request_state *state,
353                            krb5_error **err_replyptr,
354                            krb5_pa_data ***out_padata,
355                            krb5_boolean *retry)
356 {
357     krb5_error_code retval = 0;
358     krb5_error *err_reply = *err_replyptr;
359     *out_padata = NULL;
360     *retry = 0;
361     if (state->armor_key) {
362         krb5_pa_data *fx_error_pa;
363         krb5_pa_data **result = NULL;
364         krb5_data scratch, *encoded_td = NULL;
365         krb5_error *fx_error = NULL;
366         krb5_fast_response *fast_response = NULL;
367         retval = decode_krb5_padata_sequence(&err_reply->e_data, &result);
368         if (retval == 0)
369             retval = decrypt_fast_reply(context, state, result, &fast_response);
370         if (retval) {
371             /*This can happen if the KDC does not understand FAST. We
372              * don't expect that, but treating it as the fatal error
373              * indicated by the KDC seems reasonable.
374              */
375             *retry = 0;
376             krb5_free_pa_data(context, result);
377             return 0;
378         }
379         krb5_free_pa_data(context, result);
380         result = NULL;
381         if (retval == 0) {
382             fx_error_pa = krb5int_find_pa_data(context, fast_response->padata, KRB5_PADATA_FX_ERROR);
383             if (fx_error_pa == NULL) {
384                 krb5_set_error_message(context, KRB5KDC_ERR_PREAUTH_FAILED, "Expecting FX_ERROR pa-data inside FAST container");
385                 retval = KRB5KDC_ERR_PREAUTH_FAILED;
386             }
387         }
388         if (retval == 0) {
389             scratch.data = (char *) fx_error_pa->contents;
390             scratch.length = fx_error_pa->length;
391             retval = decode_krb5_error(&scratch, &fx_error);
392         }
393         /*
394          * krb5_pa_data and krb5_typed_data are safe to cast between:
395          * they have the same type fields in the same order.
396          * (krb5_preauthtype is a krb5_int32).  If krb5_typed_data is
397          * ever changed then this will need to be a copy not a cast.
398          */
399         if (retval == 0)
400             retval = encode_krb5_typed_data( (const krb5_typed_data **)fast_response->padata,
401                                              &encoded_td);
402         if (retval == 0) {
403             fx_error->e_data = *encoded_td;
404             free(encoded_td); /*contents owned by fx_error*/
405             encoded_td = NULL;
406             krb5_free_error(context, err_reply);
407             *err_replyptr = fx_error;
408             fx_error = NULL;
409             *out_padata = fast_response->padata;
410             fast_response->padata = NULL;
411             /*
412              * If there is more than the fx_error padata, then we want
413              * to retry the error if a cookie is present
414              */
415             *retry = (*out_padata)[1] != NULL;
416             if (krb5int_find_pa_data(context, *out_padata, KRB5_PADATA_FX_COOKIE) == NULL)
417                 *retry = 0;
418         }
419         if (fx_error)
420             krb5_free_error(context, fx_error);
421         krb5_free_fast_response(context, fast_response);
422     } else { /*not FAST*/
423         *retry = (err_reply->e_data.length > 0);
424         if ((err_reply->error == KDC_ERR_PREAUTH_REQUIRED
425              ||err_reply->error == KDC_ERR_PREAUTH_FAILED) && err_reply->e_data.length) {
426             krb5_pa_data **result = NULL;
427             retval = decode_krb5_padata_sequence(&err_reply->e_data, &result);
428             if (retval == 0)
429                 if (retval == 0) {
430                     *out_padata = result;
431
432                     return 0;
433                 }
434             krb5_free_pa_data(context, result);
435             krb5_set_error_message(context, retval,
436                                    "Error decoding padata in error reply");
437             return retval;
438         }
439     }
440     return retval;
441 }
442
443
444 krb5_error_code
445 krb5int_fast_process_response(krb5_context context,
446                               struct krb5int_fast_request_state *state,
447                               krb5_kdc_rep *resp,
448                               krb5_keyblock **strengthen_key)
449 {
450     krb5_error_code retval = 0;
451     krb5_fast_response *fast_response = NULL;
452     krb5_data *encoded_ticket = NULL;
453     krb5_boolean cksum_valid;
454     krb5_clear_error_message(context);
455     *strengthen_key = NULL;
456     if (state->armor_key == 0)
457         return 0;
458     retval = decrypt_fast_reply(context, state, resp->padata,
459                                 &fast_response);
460     if (retval == 0) {
461         if (fast_response->finished == 0) {
462             retval = KRB5_KDCREP_MODIFIED;
463             krb5_set_error_message(context, retval, "FAST response missing finish message in KDC reply");
464         }
465     }
466     if (retval == 0)
467         retval = encode_krb5_ticket(resp->ticket, &encoded_ticket);
468     if (retval == 0)
469         retval = krb5_c_verify_checksum(context, state->armor_key,
470                                         KRB5_KEYUSAGE_FAST_FINISHED,
471                                         encoded_ticket,
472                                         &fast_response->finished->ticket_checksum,
473                                         &cksum_valid);
474     if (retval == 0 && cksum_valid == 0) {
475         retval = KRB5_KDCREP_MODIFIED;
476         krb5_set_error_message(context, retval, "ticket modified in KDC reply");
477     }
478     if (retval == 0) {
479         krb5_free_principal(context, resp->client);
480         resp->client = fast_response->finished->client;
481         fast_response->finished->client = NULL;
482         *strengthen_key = fast_response->strengthen_key;
483         fast_response->strengthen_key = NULL;
484         krb5_free_pa_data(context, resp->padata);
485         resp->padata = fast_response->padata;
486         fast_response->padata = NULL;
487     }
488     if (fast_response)
489         krb5_free_fast_response(context, fast_response);
490     if (encoded_ticket)
491         krb5_free_data(context, encoded_ticket);
492     return retval;
493 }
494
495 krb5_error_code
496 krb5int_fast_reply_key(krb5_context context,
497                        krb5_keyblock *strengthen_key,
498                        krb5_keyblock *existing_key,
499                        krb5_keyblock *out_key)
500 {
501     krb5_keyblock *key = NULL;
502     krb5_error_code retval = 0;
503     krb5_free_keyblock_contents(context, out_key);
504     if (strengthen_key) {
505         retval = krb5_c_fx_cf2_simple(context, strengthen_key,
506                                       "strengthenkey", existing_key, "replykey", &key);
507         if (retval == 0) {
508             *out_key = *key;
509             free(key);
510         }
511     } else {
512         retval = krb5_copy_keyblock_contents(context, existing_key, out_key);
513     }
514     return retval;
515 }
516
517
518 krb5_error_code
519 krb5int_fast_make_state( krb5_context context, struct krb5int_fast_request_state **state)
520 {
521     struct krb5int_fast_request_state *local_state ;
522     local_state = malloc(sizeof *local_state);
523     if (local_state == NULL)
524         return ENOMEM;
525     memset(local_state, 0, sizeof(*local_state));
526     *state = local_state;
527     return 0;
528 }
529
530 void
531 krb5int_fast_free_state( krb5_context context, struct krb5int_fast_request_state *state)
532 {
533     /*We are responsible for none of the store in the fast_outer_req*/
534     krb5_free_keyblock(context, state->armor_key);
535     krb5_free_fast_armor(context, state->armor);
536     free(state);
537 }
538
539 krb5_pa_data *
540 krb5int_find_pa_data(krb5_context context, krb5_pa_data *const *padata,
541                      krb5_preauthtype pa_type)
542 {
543     krb5_pa_data * const *tmppa;
544
545     if (padata == NULL)
546         return NULL;
547
548     for (tmppa = padata; *tmppa != NULL; tmppa++) {
549         if ((*tmppa)->pa_type == pa_type)
550             break;
551     }
552
553     return *tmppa;
554 }
555
556
557 krb5_error_code
558 krb5int_fast_verify_nego(krb5_context context,
559                          struct krb5int_fast_request_state *state,
560                          krb5_kdc_rep *rep, krb5_data *request,
561                          krb5_keyblock *decrypting_key,
562                          krb5_boolean *fast_avail)
563 {
564     krb5_error_code retval = 0;
565     krb5_checksum *checksum = NULL;
566     krb5_pa_data *pa;
567     krb5_data scratch;
568     krb5_boolean valid;
569
570     if (rep->enc_part2->flags& TKT_FLG_ENC_PA_REP) {
571         pa = krb5int_find_pa_data(context, rep->enc_part2->enc_padata,
572                                   KRB5_ENCPADATA_REQ_ENC_PA_REP);
573         if (pa == NULL)
574             retval = KRB5_KDCREP_MODIFIED;
575         else {
576             scratch.data = (char *) pa->contents;
577             scratch.length = pa->length;
578         }
579         if (retval == 0)
580             retval = decode_krb5_checksum(&scratch, &checksum);
581         if (retval == 0)
582             retval = krb5_c_verify_checksum(context, decrypting_key,
583                                             KRB5_KEYUSAGE_AS_REQ,
584                                             request, checksum, &valid);
585         if (retval == 0 &&valid == 0)
586             retval = KRB5_KDCREP_MODIFIED;
587         if (retval == 0) {
588             pa = krb5int_find_pa_data(context, rep->enc_part2->enc_padata,
589                                       KRB5_PADATA_FX_FAST);
590             *fast_avail = (pa != NULL);
591         }
592     }
593     if (checksum)
594         krb5_free_checksum(context, checksum);
595     return retval;
596 }
597
598 krb5_boolean
599 krb5int_upgrade_to_fast_p(krb5_context context,
600                           struct krb5int_fast_request_state *state,
601                           krb5_pa_data **padata)
602 {
603     if (state->armor_key != NULL)
604         return 0; /*already using FAST*/
605     if (!(state->fast_state_flags & KRB5INT_FAST_ARMOR_AVAIL))
606         return 0;
607     if (krb5int_find_pa_data(context, padata, KRB5_PADATA_FX_FAST) != NULL) {
608         state->fast_state_flags |= KRB5INT_FAST_DO_FAST;
609         return 1;
610     }
611     return 0;
612 }