Merge branch 'master' into tlv-mic
[moonshot.git] / mech_eap / util.h
1 /*
2  * Copyright (c) 2011, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of JANET(UK) nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 /*
33  * Portions Copyright 2003-2010 Massachusetts Institute of Technology.
34  * All Rights Reserved.
35  *
36  * Export of this software from the United States of America may
37  *   require a specific license from the United States Government.
38  *   It is the responsibility of any person or organization contemplating
39  *   export to obtain such a license before exporting.
40  *
41  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
42  * distribute this software and its documentation for any purpose and
43  * without fee is hereby granted, provided that the above copyright
44  * notice appear in all copies and that both that copyright notice and
45  * this permission notice appear in supporting documentation, and that
46  * the name of M.I.T. not be used in advertising or publicity pertaining
47  * to distribution of the software without specific, written prior
48  * permission.  Furthermore if you modify this software you must label
49  * your software as modified software and not distribute it in such a
50  * fashion that it might be confused with the original M.I.T. software.
51  * M.I.T. makes no representations about the suitability of
52  * this software for any purpose.  It is provided "as is" without express
53  * or implied warranty.
54  *
55  */
56
57 /*
58  * Utility functions.
59  */
60
61 #ifndef _UTIL_H_
62 #define _UTIL_H_ 1
63
64 #include <sys/param.h>
65 #include <string.h>
66 #include <errno.h>
67
68 #include <krb5.h>
69
70 #ifdef __cplusplus
71 extern "C" {
72 #endif
73
74 #ifndef MIN
75 #define MIN(_a,_b)  ((_a)<(_b)?(_a):(_b))
76 #endif
77
78 #if !(defined(__cplusplus)) || (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))
79 #define GSSEAP_UNUSED __attribute__ ((__unused__)) 
80 #else
81 #define GSSEAP_UNUSED
82 #endif
83
84 /* util_buffer.c */
85 OM_uint32
86 makeStringBuffer(OM_uint32 *minor,
87                  const char *string,
88                  gss_buffer_t buffer);
89
90 OM_uint32
91 bufferToString(OM_uint32 *minor,
92                const gss_buffer_t buffer,
93                char **pString);
94
95 OM_uint32
96 duplicateBuffer(OM_uint32 *minor,
97                 const gss_buffer_t src,
98                 gss_buffer_t dst);
99
100 static inline int
101 bufferEqual(const gss_buffer_t b1, const gss_buffer_t b2)
102 {
103     return (b1->length == b2->length &&
104             memcmp(b1->value, b2->value, b2->length) == 0);
105 }
106
107 static inline int
108 bufferEqualString(const gss_buffer_t b1, const char *s)
109 {
110     gss_buffer_desc b2;
111
112     b2.length = strlen(s);
113     b2.value = (char *)s;
114
115     return bufferEqual(b1, &b2);
116 }
117
118 /* util_cksum.c */
119 int
120 gssEapSign(krb5_context context,
121            krb5_cksumtype type,
122            size_t rrc,
123 #ifdef HAVE_HEIMDAL_VERSION
124            krb5_crypto crypto,
125 #else
126            krb5_keyblock *key,
127 #endif
128            krb5_keyusage sign_usage,
129            gss_iov_buffer_desc *iov,
130            int iov_count);
131
132 int
133 gssEapVerify(krb5_context context,
134              krb5_cksumtype type,
135              size_t rrc,
136 #ifdef HAVE_HEIMDAL_VERSION
137              krb5_crypto crypto,
138 #else
139              krb5_keyblock *key,
140 #endif
141              krb5_keyusage sign_usage,
142              gss_iov_buffer_desc *iov,
143              int iov_count,
144              int *valid);
145
146 #if 0
147 OM_uint32
148 gssEapEncodeGssChannelBindings(OM_uint32 *minor,
149                                gss_channel_bindings_t chanBindings,
150                                gss_buffer_t encodedBindings);
151 #endif
152
153 /* util_context.c */
154 #define EAP_EXPORT_CONTEXT_V1           1
155
156 enum gss_eap_token_type {
157     TOK_TYPE_NONE                    = 0x0000,  /* no token */
158     TOK_TYPE_MIC                     = 0x0404,  /* RFC 4121 MIC token */
159     TOK_TYPE_WRAP                    = 0x0504,  /* RFC 4121 wrap token */
160     TOK_TYPE_EXPORT_NAME             = 0x0401,  /* RFC 2743 exported name */
161     TOK_TYPE_EXPORT_NAME_COMPOSITE   = 0x0402,  /* exported composite name */
162     TOK_TYPE_DELETE_CONTEXT          = 0x0405,  /* RFC 2743 delete context */
163     TOK_TYPE_INITIATOR_CONTEXT       = 0x0601,  /* initiator-sent context token */
164     TOK_TYPE_ACCEPTOR_CONTEXT        = 0x0602,  /* acceptor-sent context token */
165 };
166
167 struct gss_eap_itok_map {
168     OM_uint32 type;                             /* inner token type */
169     OM_uint32 flag;                             /* context flag */
170 };
171
172 /* inner token types and flags */
173 #define ITOK_TYPE_NONE                      0x00000000
174 #define ITOK_TYPE_CONTEXT_ERR               0x00000001 /* critical */
175 #define ITOK_TYPE_ACCEPTOR_NAME_REQ         0x00000002 /* TBD */
176 #define ITOK_TYPE_ACCEPTOR_NAME_RESP        0x00000003 /* TBD */
177 #define ITOK_TYPE_EAP_RESP                  0x00000004 /* critical, required, if not reauth */
178 #define ITOK_TYPE_EAP_REQ                   0x00000005 /* critical, required, if not reauth */
179 #define ITOK_TYPE_GSS_CHANNEL_BINDINGS      0x00000006 /* critical, required, if not reauth */
180 #define ITOK_TYPE_REAUTH_CREDS              0x00000007 /* optional */
181 #define ITOK_TYPE_REAUTH_REQ                0x00000008 /* optional */
182 #define ITOK_TYPE_REAUTH_RESP               0x00000009 /* optional */
183 #define ITOK_TYPE_INITIATOR_MIC             0x0000000A /* required */
184 #define ITOK_TYPE_ACCEPTOR_MIC              0x0000000B /* required */
185 #define ITOK_TYPE_SUPPORTED_ACCEPTOR_EXTS   0x0000000C /* optional */
186 #define ITOK_TYPE_SUPPORTED_INITIATOR_EXTS  0x0000000D /* optional */
187 #define ITOK_TYPE_VERSION_INFO              0x0000000E /* optional */
188 #define ITOK_TYPE_VENDOR_INFO               0x0000000F /* optional */
189
190 #define ITOK_FLAG_CRITICAL                  0x80000000  /* critical, wire flag */
191 #define ITOK_FLAG_VERIFIED                  0x40000000  /* verified, API flag */
192
193 #define ITOK_TYPE_MASK                      (~(ITOK_FLAG_CRITICAL | ITOK_FLAG_VERIFIED))
194
195 #define ITOK_HEADER_LENGTH                  8           /* type || length */
196
197 OM_uint32 gssEapAllocContext(OM_uint32 *minor, gss_ctx_id_t *pCtx);
198 OM_uint32 gssEapReleaseContext(OM_uint32 *minor, gss_ctx_id_t *pCtx);
199
200 OM_uint32
201 gssEapContextTime(OM_uint32 *minor,
202                   gss_ctx_id_t context_handle,
203                   OM_uint32 *time_rec);
204
205 OM_uint32
206 gssEapGetConversationMIC(OM_uint32 *minor,
207                          gss_ctx_id_t ctx,
208                          gss_buffer_t convMIC);
209
210 OM_uint32
211 gssEapVerifyConversationMIC(OM_uint32 *minor,
212                             gss_ctx_id_t ctx,
213                             const gss_buffer_t convMIC);
214
215 OM_uint32
216 gssEapMakeTokenChannelBindings(OM_uint32 *minor,
217                                gss_ctx_id_t ctx,
218                                gss_channel_bindings_t userBindings,
219                                gss_buffer_t inputToken,
220                                gss_channel_bindings_t wireBindings);
221
222 /* util_cred.c */
223 OM_uint32 gssEapAllocCred(OM_uint32 *minor, gss_cred_id_t *pCred);
224 OM_uint32 gssEapReleaseCred(OM_uint32 *minor, gss_cred_id_t *pCred);
225
226 OM_uint32
227 gssEapAcquireCred(OM_uint32 *minor,
228                   const gss_name_t desiredName,
229                   const gss_buffer_t password,
230                   OM_uint32 timeReq,
231                   const gss_OID_set desiredMechs,
232                   int cred_usage,
233                   gss_cred_id_t *pCred,
234                   gss_OID_set *pActualMechs,
235                   OM_uint32 *timeRec);
236
237 int gssEapCredAvailable(gss_cred_id_t cred, gss_OID mech);
238
239 /* util_crypt.c */
240 int
241 gssEapEncrypt(krb5_context context, int dce_style, size_t ec,
242               size_t rrc,
243 #ifdef HAVE_HEIMDAL_VERSION
244               krb5_crypto crypto,
245 #else
246               krb5_keyblock *key,
247 #endif
248               int usage,
249               gss_iov_buffer_desc *iov, int iov_count);
250
251 int
252 gssEapDecrypt(krb5_context context, int dce_style, size_t ec,
253               size_t rrc,
254 #ifdef HAVE_HEIMDAL_VERSION
255               krb5_crypto crypto,
256 #else
257               krb5_keyblock *key,
258 #endif
259               int usage,
260               gss_iov_buffer_desc *iov, int iov_count);
261
262 int
263 gssEapMapCryptoFlag(OM_uint32 type);
264
265 gss_iov_buffer_t
266 gssEapLocateIov(gss_iov_buffer_desc *iov,
267                 int iov_count,
268                 OM_uint32 type);
269
270 void
271 gssEapIovMessageLength(gss_iov_buffer_desc *iov,
272                        int iov_count,
273                        size_t *data_length,
274                        size_t *assoc_data_length);
275
276 void
277 gssEapReleaseIov(gss_iov_buffer_desc *iov, int iov_count);
278
279 int
280 gssEapIsIntegrityOnly(gss_iov_buffer_desc *iov, int iov_count);
281
282 int
283 gssEapAllocIov(gss_iov_buffer_t iov, size_t size);
284
285 OM_uint32
286 gssEapDeriveRfc3961Key(OM_uint32 *minor,
287                        const unsigned char *key,
288                        size_t keyLength,
289                        krb5_enctype enctype,
290                        krb5_keyblock *pKey);
291
292 /* util_krb.c */
293 #ifdef HAVE_HEIMDAL_VERSION
294
295 #define KRB_TIME_FOREVER        ((time_t)~0L)
296
297 #define KRB_KEY_TYPE(key)       ((key)->keytype)
298 #define KRB_KEY_DATA(key)       ((key)->keyvalue.data)
299 #define KRB_KEY_LENGTH(key)     ((key)->keyvalue.length)
300
301 #define KRB_PRINC_LENGTH(princ) ((princ)->name.name_string.len)
302 #define KRB_PRINC_TYPE(princ)   ((princ)->name.name_type)
303 #define KRB_PRINC_NAME(princ)   ((princ)->name.name_string.val)
304 #define KRB_PRINC_REALM(princ)  ((princ)->realm)
305
306 #define KRB_KT_ENT_KEYBLOCK(e)  (&(e)->keyblock)
307 #define KRB_KT_ENT_FREE(c, e)   krb5_kt_free_entry((c), (e))
308
309 #define KRB_CRYPTO_CONTEXT(ctx) (krbCrypto)
310
311 #else
312
313 #define KRB_TIME_FOREVER        KRB5_INT32_MAX
314
315 #define KRB_KEY_TYPE(key)       ((key)->enctype)
316 #define KRB_KEY_DATA(key)       ((key)->contents)
317 #define KRB_KEY_LENGTH(key)     ((key)->length)
318
319 #define KRB_PRINC_LENGTH(princ) (krb5_princ_size(NULL, (princ)))
320 #define KRB_PRINC_TYPE(princ)   (krb5_princ_type(NULL, (princ)))
321 #define KRB_PRINC_NAME(princ)   (krb5_princ_name(NULL, (princ)))
322 #define KRB_PRINC_REALM(princ)  (krb5_princ_realm(NULL, (princ)))
323
324 #define KRB_KT_ENT_KEYBLOCK(e)  (&(e)->key)
325 #define KRB_KT_ENT_FREE(c, e)   krb5_free_keytab_entry_contents((c), (e))
326
327 #define KRB_CRYPTO_CONTEXT(ctx) (&(ctx)->rfc3961Key)
328
329 #endif /* HAVE_HEIMDAL_VERSION */
330
331 #define KRB_KEY_INIT(key)       do {        \
332         KRB_KEY_TYPE(key) = ENCTYPE_NULL;   \
333         KRB_KEY_DATA(key) = NULL;           \
334         KRB_KEY_LENGTH(key) = 0;            \
335     } while (0)
336
337 #define GSSEAP_KRB_INIT(ctx) do {                   \
338         OM_uint32 tmpMajor;                         \
339                                                     \
340         tmpMajor  = gssEapKerberosInit(minor, ctx); \
341         if (GSS_ERROR(tmpMajor)) {                  \
342             return tmpMajor;                        \
343         }                                           \
344     } while (0)
345
346 OM_uint32
347 gssEapKerberosInit(OM_uint32 *minor, krb5_context *context);
348
349 OM_uint32
350 rfc3961ChecksumTypeForKey(OM_uint32 *minor,
351                           krb5_keyblock *key,
352                           krb5_cksumtype *cksumtype);
353
354 krb5_const_principal
355 krbAnonymousPrincipal(void);
356
357 krb5_error_code
358 krbCryptoLength(krb5_context krbContext,
359 #ifdef HAVE_HEIMDAL_VERSION
360                 krb5_crypto krbCrypto,
361 #else
362                 krb5_keyblock *key,
363 #endif
364                 int type,
365                 size_t *length);
366
367 krb5_error_code
368 krbPaddingLength(krb5_context krbContext,
369 #ifdef HAVE_HEIMDAL_VERSION
370                  krb5_crypto krbCrypto,
371 #else
372                  krb5_keyblock *key,
373 #endif
374                  size_t dataLength,
375                  size_t *padLength);
376
377 krb5_error_code
378 krbBlockSize(krb5_context krbContext,
379 #ifdef HAVE_HEIMDAL_VERSION
380                  krb5_crypto krbCrypto,
381 #else
382                  krb5_keyblock *key,
383 #endif
384                  size_t *blockSize);
385
386 krb5_error_code
387 krbEnctypeToString(krb5_context krbContext,
388                    krb5_enctype enctype,
389                    const char *prefix,
390                    gss_buffer_t string);
391
392 krb5_error_code
393 krbMakeAuthDataKdcIssued(krb5_context context,
394                          const krb5_keyblock *key,
395                          krb5_const_principal issuer,
396 #ifdef HAVE_HEIMDAL_VERSION
397                          const AuthorizationData *authdata,
398                          AuthorizationData *adKdcIssued
399 #else
400                          krb5_authdata *const *authdata,
401                          krb5_authdata ***adKdcIssued
402 #endif
403                          );
404
405 krb5_error_code
406 krbMakeCred(krb5_context context,
407             krb5_auth_context authcontext,
408             krb5_creds *creds,
409             krb5_data *data);
410
411 /* util_lucid.c */
412 OM_uint32
413 gssEapExportLucidSecContext(OM_uint32 *minor,
414                             gss_ctx_id_t ctx,
415                             const gss_OID desiredObject,
416                             gss_buffer_set_t *data_set);
417
418 /* util_mech.c */
419 extern gss_OID GSS_EAP_MECHANISM;
420
421 int
422 gssEapInternalizeOid(const gss_OID oid,
423                      gss_OID *const pInternalizedOid);
424
425 OM_uint32
426 gssEapReleaseOid(OM_uint32 *minor, gss_OID *oid);
427
428 OM_uint32
429 gssEapDefaultMech(OM_uint32 *minor,
430                   gss_OID *oid);
431
432 OM_uint32
433 gssEapIndicateMechs(OM_uint32 *minor,
434                     gss_OID_set *mechs);
435
436 OM_uint32
437 gssEapEnctypeToOid(OM_uint32 *minor,
438                    krb5_enctype enctype,
439                    gss_OID *pOid);
440
441 OM_uint32
442 gssEapOidToEnctype(OM_uint32 *minor,
443                    const gss_OID oid,
444                    krb5_enctype *enctype);
445
446 int
447 gssEapIsMechanismOid(const gss_OID oid);
448
449 int
450 gssEapIsConcreteMechanismOid(const gss_OID oid);
451
452 OM_uint32
453 gssEapValidateMechs(OM_uint32 *minor,
454                    const gss_OID_set mechs);
455
456 gss_buffer_t
457 gssEapOidToSaslName(const gss_OID oid);
458
459 gss_OID
460 gssEapSaslNameToOid(const gss_buffer_t name);
461
462 /* util_name.c */
463 #define EXPORT_NAME_FLAG_OID        0x1
464 #define EXPORT_NAME_FLAG_COMPOSITE  0x2
465
466 OM_uint32 gssEapAllocName(OM_uint32 *minor, gss_name_t *pName);
467 OM_uint32 gssEapReleaseName(OM_uint32 *minor, gss_name_t *pName);
468 OM_uint32 gssEapExportName(OM_uint32 *minor,
469                            const gss_name_t name,
470                            gss_buffer_t exportedName);
471 OM_uint32 gssEapExportNameInternal(OM_uint32 *minor,
472                                    const gss_name_t name,
473                                    gss_buffer_t exportedName,
474                                    unsigned int flags);
475 OM_uint32 gssEapImportName(OM_uint32 *minor,
476                            const gss_buffer_t input_name_buffer,
477                            gss_OID input_name_type,
478                            gss_name_t *output_name);
479 OM_uint32 gssEapImportNameInternal(OM_uint32 *minor,
480                                    const gss_buffer_t input_name_buffer,
481                                    gss_name_t *output_name,
482                                    unsigned int flags);
483 OM_uint32
484 gssEapDuplicateName(OM_uint32 *minor,
485                     const gss_name_t input_name,
486                     gss_name_t *dest_name);
487
488 OM_uint32
489 gssEapDisplayName(OM_uint32 *minor,
490                   gss_name_t name,
491                   gss_buffer_t output_name_buffer,
492                   gss_OID *output_name_type);
493
494 /* util_oid.c */
495 OM_uint32
496 composeOid(OM_uint32 *minor_status,
497            const char *prefix,
498            size_t prefix_len,
499            int suffix,
500            gss_OID_desc *oid);
501
502 OM_uint32
503 decomposeOid(OM_uint32 *minor_status,
504              const char *prefix,
505              size_t prefix_len,
506              gss_OID_desc *oid,
507              int *suffix) ;
508
509 OM_uint32
510 duplicateOid(OM_uint32 *minor_status,
511              const gss_OID_desc * const oid,
512              gss_OID *new_oid);
513
514 OM_uint32
515 duplicateOidSet(OM_uint32 *minor,
516                 const gss_OID_set src,
517                 gss_OID_set *dst);
518
519 static inline int
520 oidEqual(const gss_OID_desc *o1, const gss_OID_desc *o2)
521 {
522     if (o1 == GSS_C_NO_OID)
523         return (o2 == GSS_C_NO_OID);
524     else if (o2 == GSS_C_NO_OID)
525         return (o1 == GSS_C_NO_OID);
526     else
527         return (o1->length == o2->length &&
528                 memcmp(o1->elements, o2->elements, o1->length) == 0);
529 }
530
531 /* util_ordering.c */
532 OM_uint32
533 sequenceInternalize(OM_uint32 *minor,
534                     void **vqueue,
535                     unsigned char **buf,
536                     size_t *lenremain);
537
538 OM_uint32
539 sequenceExternalize(OM_uint32 *minor,
540                     void *vqueue,
541                     unsigned char **buf,
542                     size_t *lenremain);
543
544 size_t
545 sequenceSize(void *vqueue);
546
547 OM_uint32
548 sequenceFree(OM_uint32 *minor, void **vqueue);
549
550 OM_uint32
551 sequenceCheck(OM_uint32 *minor, void **vqueue, uint64_t seqnum);
552
553 OM_uint32
554 sequenceInit(OM_uint32 *minor, void **vqueue, uint64_t seqnum,
555              int do_replay, int do_sequence, int wide_nums);
556
557 /* util_sm.c */
558 enum gss_eap_state {
559     GSSEAP_STATE_INITIAL        = 0x01,     /* initial state */
560     GSSEAP_STATE_AUTHENTICATE   = 0x02,     /* exchange EAP messages */
561     GSSEAP_STATE_INITIATOR_EXTS = 0x04,     /* initiator extensions */
562     GSSEAP_STATE_ACCEPTOR_EXTS  = 0x08,     /* acceptor extensions */
563 #ifdef GSSEAP_ENABLE_REAUTH
564     GSSEAP_STATE_REAUTHENTICATE = 0x10,     /* GSS reauthentication messages */
565 #endif
566     GSSEAP_STATE_ESTABLISHED    = 0x20,     /* context established */
567     GSSEAP_STATE_ALL            = 0x3F
568 };
569
570 #define GSSEAP_STATE_NEXT(s)    ((s) << 1)
571
572 #define GSSEAP_SM_STATE(ctx)                ((ctx)->state)
573
574 #ifdef GSSEAP_DEBUG
575 void gssEapSmTransition(gss_ctx_id_t ctx, enum gss_eap_state state);
576 #define GSSEAP_SM_TRANSITION(ctx, state)    gssEapSmTransition((ctx), (state))
577 #else
578 #define GSSEAP_SM_TRANSITION(ctx, newstate)    do { (ctx)->state = (newstate); } while (0)
579 #endif
580
581 #define GSSEAP_SM_TRANSITION_NEXT(ctx)      GSSEAP_SM_TRANSITION((ctx), GSSEAP_STATE_NEXT(GSSEAP_SM_STATE((ctx))))
582
583 /* state machine entry */
584 struct gss_eap_sm {
585     OM_uint32 inputTokenType;
586     OM_uint32 outputTokenType;
587     enum gss_eap_state validStates;
588     OM_uint32 itokFlags;
589     OM_uint32 (*processToken)(OM_uint32 *,
590                               gss_cred_id_t,
591                               gss_ctx_id_t,
592                               gss_name_t,
593                               gss_OID,
594                               OM_uint32,
595                               OM_uint32,
596                               gss_channel_bindings_t,
597                               gss_buffer_t,
598                               gss_buffer_t,
599                               OM_uint32 *);
600 };
601
602 /* state machine flags, set by handler */
603 #define SM_FLAG_SEND_TOKEN                  0x00000001  /* exit state machine, send token */
604 #define SM_FLAG_OUTPUT_TOKEN_CRITICAL       0x00000002  /* output token is critical */
605
606 /* state machine flags, set by state machine */
607 #define SM_FLAG_INPUT_TOKEN_CRITICAL        0x10000000  /* input token was critical */
608
609 #define SM_ITOK_FLAG_REQUIRED               0x00000001  /* received tokens must be present */
610
611 OM_uint32
612 gssEapSmStep(OM_uint32 *minor,
613              gss_cred_id_t cred,
614              gss_ctx_id_t ctx,
615              gss_name_t target,
616              gss_OID mech,
617              OM_uint32 reqFlags,
618              OM_uint32 timeReq,
619              gss_channel_bindings_t chanBindings,
620              gss_buffer_t inputToken,
621              gss_buffer_t outputToken,
622              struct gss_eap_sm *sm,
623              size_t smCount);
624
625 void
626 gssEapSmTransition(gss_ctx_id_t ctx, enum gss_eap_state state);
627
628 /* util_token.c */
629 OM_uint32
630 gssEapDecodeInnerTokens(OM_uint32 *minor,
631                         const gss_buffer_t buffer,
632                         gss_buffer_set_t *pExtensions,
633                         OM_uint32 **pTypes);
634
635 OM_uint32
636 gssEapRecordContextTokenHeader(OM_uint32 *minor,
637                                gss_ctx_id_t ctx,
638                                enum gss_eap_token_type tokType);
639
640 OM_uint32
641 gssEapRecordInnerContextToken(OM_uint32 *minor,
642                               gss_ctx_id_t ctx,
643                               gss_buffer_t innerToken,
644                               OM_uint32 type);
645
646 OM_uint32
647 gssEapVerifyContextToken(OM_uint32 *minor,
648                          gss_ctx_id_t ctx,
649                          const gss_buffer_t inputToken,
650                          enum gss_eap_token_type tokenType,
651                          gss_buffer_t innerInputToken);
652
653 OM_uint32
654 gssEapEncodeSupportedExts(OM_uint32 *minor,
655                           OM_uint32 *types,
656                           size_t typesCount,
657                           gss_buffer_t outputToken);
658
659 OM_uint32
660 gssEapProcessSupportedExts(OM_uint32 *minor,
661                            gss_buffer_t inputToken,
662                            struct gss_eap_itok_map *map,
663                            size_t mapCount,
664                            OM_uint32 *flags);
665
666 size_t
667 tokenSize(size_t bodySize);
668
669 void
670 makeTokenHeader(size_t body_size,
671                 unsigned char **buf);
672
673 OM_uint32
674 verifyTokenHeader(OM_uint32 *minor,
675                   gss_OID mech,
676                   size_t *body_size,
677                   unsigned char **buf_in,
678                   size_t toksize_in,
679                   enum gss_eap_token_type *ret_tok_type);
680
681 /* Helper macros */
682
683 #define GSSEAP_CALLOC                   calloc
684 #define GSSEAP_MALLOC                   malloc
685 #define GSSEAP_FREE                     free
686 #define GSSEAP_REALLOC                  realloc
687
688 #define GSSEAP_NOT_IMPLEMENTED          do {            \
689         assert(0 && "not implemented");                 \
690         *minor = ENOSYS;                                \
691         return GSS_S_FAILURE;                           \
692     } while (0)
693
694 #include <pthread.h>
695
696 #define GSSEAP_MUTEX                    pthread_mutex_t
697 #define GSSEAP_MUTEX_INITIALIZER        PTHREAD_MUTEX_INITIALIZER
698
699 #define GSSEAP_MUTEX_INIT(m)            pthread_mutex_init((m), NULL)
700 #define GSSEAP_MUTEX_DESTROY(m)         pthread_mutex_destroy((m))
701 #define GSSEAP_MUTEX_LOCK(m)            pthread_mutex_lock((m))
702 #define GSSEAP_MUTEX_UNLOCK(m)          pthread_mutex_unlock((m))
703
704 #define GSSEAP_THREAD_KEY               pthread_key_t
705 #define GSSEAP_KEY_CREATE(k, d)         pthread_key_create((k), (d))
706 #define GSSEAP_GETSPECIFIC(k)           pthread_getspecific((k))
707 #define GSSEAP_SETSPECIFIC(k, d)        pthread_setspecific((k), (d))
708
709 #define GSSEAP_THREAD_ONCE              pthread_once_t
710 #define GSSEAP_ONCE(o, i)               pthread_once((o), (i))
711 #define GSSEAP_ONCE_INITIALIZER         PTHREAD_ONCE_INIT
712
713 /* Helper functions */
714 static inline void
715 store_uint16_be(uint16_t val, void *vp)
716 {
717     unsigned char *p = (unsigned char *)vp;
718
719     p[0] = (val >>  8) & 0xff;
720     p[1] = (val      ) & 0xff;
721 }
722
723 static inline uint16_t
724 load_uint16_be(const void *cvp)
725 {
726     const unsigned char *p = (const unsigned char *)cvp;
727
728     return (p[1] | (p[0] << 8));
729 }
730
731 static inline void
732 store_uint32_be(uint32_t val, void *vp)
733 {
734     unsigned char *p = (unsigned char *)vp;
735
736     p[0] = (val >> 24) & 0xff;
737     p[1] = (val >> 16) & 0xff;
738     p[2] = (val >>  8) & 0xff;
739     p[3] = (val      ) & 0xff;
740 }
741
742 static inline uint32_t
743 load_uint32_be(const void *cvp)
744 {
745     const unsigned char *p = (const unsigned char *)cvp;
746
747     return (p[3] | (p[2] << 8)
748             | ((uint32_t) p[1] << 16)
749             | ((uint32_t) p[0] << 24));
750 }
751
752 static inline void
753 store_uint64_be(uint64_t val, void *vp)
754 {
755     unsigned char *p = (unsigned char *)vp;
756
757     p[0] = (unsigned char)((val >> 56) & 0xff);
758     p[1] = (unsigned char)((val >> 48) & 0xff);
759     p[2] = (unsigned char)((val >> 40) & 0xff);
760     p[3] = (unsigned char)((val >> 32) & 0xff);
761     p[4] = (unsigned char)((val >> 24) & 0xff);
762     p[5] = (unsigned char)((val >> 16) & 0xff);
763     p[6] = (unsigned char)((val >>  8) & 0xff);
764     p[7] = (unsigned char)((val      ) & 0xff);
765 }
766
767 static inline uint64_t
768 load_uint64_be(const void *cvp)
769 {
770     const unsigned char *p = (const unsigned char *)cvp;
771
772     return ((uint64_t)load_uint32_be(p) << 32) | load_uint32_be(p + 4);
773 }
774
775 static inline unsigned char *
776 store_buffer(gss_buffer_t buffer, void *vp, int wide_nums)
777 {
778     unsigned char *p = (unsigned char *)vp;
779
780     if (wide_nums) {
781         store_uint64_be(buffer->length, p);
782         p += 8;
783     } else {
784         store_uint32_be(buffer->length, p);
785         p += 4;
786     }
787
788     if (buffer->value != NULL) {
789         memcpy(p, buffer->value, buffer->length);
790         p += buffer->length;
791     }
792
793     return p;
794 }
795
796 static inline unsigned char *
797 load_buffer(const void *cvp, size_t length, gss_buffer_t buffer)
798 {
799     buffer->length = 0;
800     buffer->value = GSSEAP_MALLOC(length);
801     if (buffer->value == NULL)
802         return NULL;
803     buffer->length = length;
804     memcpy(buffer->value, cvp, length);
805     return (unsigned char *)cvp + length;
806 }
807
808 static inline unsigned char *
809 store_oid(gss_OID oid, void *vp)
810 {
811     gss_buffer_desc buf;
812
813     if (oid != GSS_C_NO_OID) {
814         buf.length = oid->length;
815         buf.value = oid->elements;
816     } else {
817         buf.length = 0;
818         buf.value = NULL;
819     }
820
821     return store_buffer(&buf, vp, FALSE);
822 }
823
824 static inline void
825 krbDataToGssBuffer(krb5_data *data, gss_buffer_t buffer)
826 {
827     buffer->value = (void *)data->data;
828     buffer->length = data->length;
829 }
830
831 static inline void
832 krbPrincComponentToGssBuffer(krb5_principal krbPrinc,
833                              int index, gss_buffer_t buffer)
834 {
835 #ifdef HAVE_HEIMDAL_VERSION
836     buffer->value = (void *)KRB_PRINC_NAME(krbPrinc)[index];
837     buffer->length = strlen((char *)buffer->value);
838 #else
839     buffer->value = (void *)krb5_princ_component(NULL, krbPrinc, index)->data;
840     buffer->length = krb5_princ_component(NULL, krbPrinc, index)->length;
841 #endif /* HAVE_HEIMDAL_VERSION */
842 }
843
844 static inline void
845 krbPrincRealmToGssBuffer(krb5_principal krbPrinc, gss_buffer_t buffer)
846 {
847 #ifdef HAVE_HEIMDAL_VERSION
848     buffer->value = (void *)KRB_PRINC_REALM(krbPrinc);
849     buffer->length = strlen((char *)buffer->value);
850 #else
851     krbDataToGssBuffer(KRB_PRINC_REALM(krbPrinc), buffer);
852 #endif
853 }
854
855 static inline void
856 gssBufferToKrbData(gss_buffer_t buffer, krb5_data *data)
857 {
858     data->data = (char *)buffer->value;
859     data->length = buffer->length;
860 }
861
862 #ifdef __cplusplus
863 }
864 #endif
865
866 #include "util_attr.h"
867 #ifdef GSSEAP_ENABLE_REAUTH
868 #include "util_reauth.h"
869 #endif
870
871 #endif /* _UTIL_H_ */