add MIC symbols
[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 /* util_buffer.c */
79 OM_uint32
80 makeStringBuffer(OM_uint32 *minor,
81                  const char *string,
82                  gss_buffer_t buffer);
83
84 OM_uint32
85 bufferToString(OM_uint32 *minor,
86                const gss_buffer_t buffer,
87                char **pString);
88
89 OM_uint32
90 duplicateBuffer(OM_uint32 *minor,
91                 const gss_buffer_t src,
92                 gss_buffer_t dst);
93
94 static inline int
95 bufferEqual(const gss_buffer_t b1, const gss_buffer_t b2)
96 {
97     return (b1->length == b2->length &&
98             memcmp(b1->value, b2->value, b2->length) == 0);
99 }
100
101 static inline int
102 bufferEqualString(const gss_buffer_t b1, const char *s)
103 {
104     gss_buffer_desc b2;
105
106     b2.length = strlen(s);
107     b2.value = (char *)s;
108
109     return bufferEqual(b1, &b2);
110 }
111
112 /* util_cksum.c */
113 int
114 gssEapSign(krb5_context context,
115            krb5_cksumtype type,
116            size_t rrc,
117 #ifdef HAVE_HEIMDAL_VERSION
118            krb5_crypto crypto,
119 #else
120            krb5_keyblock *key,
121 #endif
122            krb5_keyusage sign_usage,
123            gss_iov_buffer_desc *iov,
124            int iov_count);
125
126 int
127 gssEapVerify(krb5_context context,
128              krb5_cksumtype type,
129              size_t rrc,
130 #ifdef HAVE_HEIMDAL_VERSION
131              krb5_crypto crypto,
132 #else
133              krb5_keyblock *key,
134 #endif
135              krb5_keyusage sign_usage,
136              gss_iov_buffer_desc *iov,
137              int iov_count,
138              int *valid);
139
140 #if 0
141 OM_uint32
142 gssEapEncodeGssChannelBindings(OM_uint32 *minor,
143                                gss_channel_bindings_t chanBindings,
144                                gss_buffer_t encodedBindings);
145 #endif
146
147 /* util_context.c */
148 #define EAP_EXPORT_CONTEXT_V1           1
149
150 enum gss_eap_token_type {
151     TOK_TYPE_NONE                    = 0x0000,  /* no token */
152     TOK_TYPE_MIC                     = 0x0404,  /* RFC 4121 MIC token */
153     TOK_TYPE_WRAP                    = 0x0504,  /* RFC 4121 wrap token */
154     TOK_TYPE_EXPORT_NAME             = 0x0401,  /* RFC 2743 exported name */
155     TOK_TYPE_EXPORT_NAME_COMPOSITE   = 0x0402,  /* exported composite name */
156     TOK_TYPE_DELETE_CONTEXT          = 0x0405,  /* RFC 2743 delete context */
157     TOK_TYPE_INITIATOR_CONTEXT       = 0x0601,  /* initiator-sent context token */
158     TOK_TYPE_ACCEPTOR_CONTEXT        = 0x0602,  /* acceptor-sent context token */
159 };
160
161 /* inner token types and flags */
162 #define ITOK_TYPE_NONE                  0x00000000
163 #define ITOK_TYPE_CONTEXT_ERR           0x00000001 /* critical */
164 #define ITOK_TYPE_ACCEPTOR_NAME_REQ     0x00000002 /* TBD */
165 #define ITOK_TYPE_ACCEPTOR_NAME_RESP    0x00000003 /* TBD */
166 #define ITOK_TYPE_EAP_RESP              0x00000004 /* critical, required, if not reauth */
167 #define ITOK_TYPE_EAP_REQ               0x00000005 /* critical, required, if not reauth */
168 #define ITOK_TYPE_GSS_CHANNEL_BINDINGS  0x00000006 /* critical, required, if not reauth */
169 #define ITOK_TYPE_REAUTH_CREDS          0x00000007 /* optional */
170 #define ITOK_TYPE_REAUTH_REQ            0x00000008 /* optional */
171 #define ITOK_TYPE_REAUTH_RESP           0x00000009 /* optional */
172 #define ITOK_TYPE_INITIATOR_MIC         0x0000000A /* required */
173 #define ITOK_TYPE_ACCEPTOR_MIC          0x0000000B /* required */
174 #define ITOK_TYPE_ACCEPTOR_EXTS         0x0000000C /* optional, acceptor exts supported */
175 #define ITOK_TYPE_INITIATOR_EXTS        0x0000000D /* optional, initiator exts supported */
176 #define ITOK_TYPE_VERSION_INFO          0x0000000E /* optional */
177 #define ITOK_TYPE_VENDOR_INFO           0x0000000F /* optional */
178
179 #define ITOK_FLAG_CRITICAL              0x80000000  /* critical, wire flag */
180 #define ITOK_FLAG_VERIFIED              0x40000000  /* verified, API flag */
181
182 #define ITOK_TYPE_MASK                  (~(ITOK_FLAG_CRITICAL | ITOK_FLAG_VERIFIED))
183
184 OM_uint32 gssEapAllocContext(OM_uint32 *minor, gss_ctx_id_t *pCtx);
185 OM_uint32 gssEapReleaseContext(OM_uint32 *minor, gss_ctx_id_t *pCtx);
186
187 OM_uint32
188 gssEapMakeToken(OM_uint32 *minor,
189                 gss_ctx_id_t ctx,
190                 const gss_buffer_t innerToken,
191                 enum gss_eap_token_type tokenType,
192                 gss_buffer_t outputToken);
193
194 OM_uint32
195 gssEapVerifyToken(OM_uint32 *minor,
196                   gss_ctx_id_t ctx,
197                   const gss_buffer_t inputToken,
198                   enum gss_eap_token_type *tokenType,
199                   gss_buffer_t innerInputToken);
200
201 OM_uint32
202 gssEapContextTime(OM_uint32 *minor,
203                   gss_ctx_id_t context_handle,
204                   OM_uint32 *time_rec);
205
206 OM_uint32
207 gssEapDisplayName(OM_uint32 *minor,
208                   gss_name_t name,
209                   gss_buffer_t output_name_buffer,
210                   gss_OID *output_name_type);
211
212 /* util_cred.c */
213 OM_uint32 gssEapAllocCred(OM_uint32 *minor, gss_cred_id_t *pCred);
214 OM_uint32 gssEapReleaseCred(OM_uint32 *minor, gss_cred_id_t *pCred);
215
216 OM_uint32
217 gssEapAcquireCred(OM_uint32 *minor,
218                   const gss_name_t desiredName,
219                   const gss_buffer_t password,
220                   OM_uint32 timeReq,
221                   const gss_OID_set desiredMechs,
222                   int cred_usage,
223                   gss_cred_id_t *pCred,
224                   gss_OID_set *pActualMechs,
225                   OM_uint32 *timeRec);
226
227 int gssEapCredAvailable(gss_cred_id_t cred, gss_OID mech);
228
229 /* util_crypt.c */
230 int
231 gssEapEncrypt(krb5_context context, int dce_style, size_t ec,
232               size_t rrc,
233 #ifdef HAVE_HEIMDAL_VERSION
234               krb5_crypto crypto,
235 #else
236               krb5_keyblock *key,
237 #endif
238               int usage,
239               gss_iov_buffer_desc *iov, int iov_count);
240
241 int
242 gssEapDecrypt(krb5_context context, int dce_style, size_t ec,
243               size_t rrc,
244 #ifdef HAVE_HEIMDAL_VERSION
245               krb5_crypto crypto,
246 #else
247               krb5_keyblock *key,
248 #endif
249               int usage,
250               gss_iov_buffer_desc *iov, int iov_count);
251
252 int
253 gssEapMapCryptoFlag(OM_uint32 type);
254
255 gss_iov_buffer_t
256 gssEapLocateIov(gss_iov_buffer_desc *iov,
257                 int iov_count,
258                 OM_uint32 type);
259
260 void
261 gssEapIovMessageLength(gss_iov_buffer_desc *iov,
262                        int iov_count,
263                        size_t *data_length,
264                        size_t *assoc_data_length);
265
266 void
267 gssEapReleaseIov(gss_iov_buffer_desc *iov, int iov_count);
268
269 int
270 gssEapIsIntegrityOnly(gss_iov_buffer_desc *iov, int iov_count);
271
272 int
273 gssEapAllocIov(gss_iov_buffer_t iov, size_t size);
274
275 OM_uint32
276 gssEapDeriveRfc3961Key(OM_uint32 *minor,
277                        const unsigned char *key,
278                        size_t keyLength,
279                        krb5_enctype enctype,
280                        krb5_keyblock *pKey);
281
282 /* util_krb.c */
283 #ifdef HAVE_HEIMDAL_VERSION
284
285 #define KRB_TIME_FOREVER        ((time_t)~0L)
286
287 #define KRB_KEY_TYPE(key)       ((key)->keytype)
288 #define KRB_KEY_DATA(key)       ((key)->keyvalue.data)
289 #define KRB_KEY_LENGTH(key)     ((key)->keyvalue.length)
290
291 #define KRB_PRINC_LENGTH(princ) ((princ)->name.name_string.len)
292 #define KRB_PRINC_TYPE(princ)   ((princ)->name.name_type)
293 #define KRB_PRINC_NAME(princ)   ((princ)->name.name_string.val)
294 #define KRB_PRINC_REALM(princ)  ((princ)->realm)
295
296 #define KRB_KT_ENT_KEYBLOCK(e)  (&(e)->keyblock)
297 #define KRB_KT_ENT_FREE(c, e)   krb5_kt_free_entry((c), (e))
298
299 #define KRB_CRYPTO_CONTEXT(ctx) (krbCrypto)
300
301 #else
302
303 #define KRB_TIME_FOREVER        KRB5_INT32_MAX
304
305 #define KRB_KEY_TYPE(key)       ((key)->enctype)
306 #define KRB_KEY_DATA(key)       ((key)->contents)
307 #define KRB_KEY_LENGTH(key)     ((key)->length)
308
309 #define KRB_PRINC_LENGTH(princ) (krb5_princ_size(NULL, (princ)))
310 #define KRB_PRINC_TYPE(princ)   (krb5_princ_type(NULL, (princ)))
311 #define KRB_PRINC_NAME(princ)   (krb5_princ_name(NULL, (princ)))
312 #define KRB_PRINC_REALM(princ)  (krb5_princ_realm(NULL, (princ)))
313
314 #define KRB_KT_ENT_KEYBLOCK(e)  (&(e)->key)
315 #define KRB_KT_ENT_FREE(c, e)   krb5_free_keytab_entry_contents((c), (e))
316
317 #define KRB_CRYPTO_CONTEXT(ctx) (&(ctx)->rfc3961Key)
318
319 #endif /* HAVE_HEIMDAL_VERSION */
320
321 #define KRB_KEY_INIT(key)       do {        \
322         KRB_KEY_TYPE(key) = ENCTYPE_NULL;   \
323         KRB_KEY_DATA(key) = NULL;           \
324         KRB_KEY_LENGTH(key) = 0;            \
325     } while (0)
326
327 #define GSSEAP_KRB_INIT(ctx) do {                   \
328         OM_uint32 tmpMajor;                         \
329                                                     \
330         tmpMajor  = gssEapKerberosInit(minor, ctx); \
331         if (GSS_ERROR(tmpMajor)) {                  \
332             return tmpMajor;                        \
333         }                                           \
334     } while (0)
335
336 OM_uint32
337 gssEapKerberosInit(OM_uint32 *minor, krb5_context *context);
338
339 OM_uint32
340 rfc3961ChecksumTypeForKey(OM_uint32 *minor,
341                           krb5_keyblock *key,
342                           krb5_cksumtype *cksumtype);
343
344 krb5_const_principal
345 krbAnonymousPrincipal(void);
346
347 krb5_error_code
348 krbCryptoLength(krb5_context krbContext,
349 #ifdef HAVE_HEIMDAL_VERSION
350                 krb5_crypto krbCrypto,
351 #else
352                 krb5_keyblock *key,
353 #endif
354                 int type,
355                 size_t *length);
356
357 krb5_error_code
358 krbPaddingLength(krb5_context krbContext,
359 #ifdef HAVE_HEIMDAL_VERSION
360                  krb5_crypto krbCrypto,
361 #else
362                  krb5_keyblock *key,
363 #endif
364                  size_t dataLength,
365                  size_t *padLength);
366
367 krb5_error_code
368 krbBlockSize(krb5_context krbContext,
369 #ifdef HAVE_HEIMDAL_VERSION
370                  krb5_crypto krbCrypto,
371 #else
372                  krb5_keyblock *key,
373 #endif
374                  size_t *blockSize);
375
376 krb5_error_code
377 krbEnctypeToString(krb5_context krbContext,
378                    krb5_enctype enctype,
379                    const char *prefix,
380                    gss_buffer_t string);
381
382 krb5_error_code
383 krbMakeAuthDataKdcIssued(krb5_context context,
384                          const krb5_keyblock *key,
385                          krb5_const_principal issuer,
386 #ifdef HAVE_HEIMDAL_VERSION
387                          const AuthorizationData *authdata,
388                          AuthorizationData *adKdcIssued
389 #else
390                          krb5_authdata *const *authdata,
391                          krb5_authdata ***adKdcIssued
392 #endif
393                          );
394
395 krb5_error_code
396 krbMakeCred(krb5_context context,
397             krb5_auth_context authcontext,
398             krb5_creds *creds,
399             krb5_data *data);
400
401 /* util_lucid.c */
402 OM_uint32
403 gssEapExportLucidSecContext(OM_uint32 *minor,
404                             gss_ctx_id_t ctx,
405                             const gss_OID desiredObject,
406                             gss_buffer_set_t *data_set);
407
408 /* util_mech.c */
409 extern gss_OID GSS_EAP_MECHANISM;
410
411 int
412 gssEapInternalizeOid(const gss_OID oid,
413                      gss_OID *const pInternalizedOid);
414
415 OM_uint32
416 gssEapReleaseOid(OM_uint32 *minor, gss_OID *oid);
417
418 OM_uint32
419 gssEapDefaultMech(OM_uint32 *minor,
420                   gss_OID *oid);
421
422 OM_uint32
423 gssEapIndicateMechs(OM_uint32 *minor,
424                     gss_OID_set *mechs);
425
426 OM_uint32
427 gssEapEnctypeToOid(OM_uint32 *minor,
428                    krb5_enctype enctype,
429                    gss_OID *pOid);
430
431 OM_uint32
432 gssEapOidToEnctype(OM_uint32 *minor,
433                    const gss_OID oid,
434                    krb5_enctype *enctype);
435
436 int
437 gssEapIsMechanismOid(const gss_OID oid);
438
439 int
440 gssEapIsConcreteMechanismOid(const gss_OID oid);
441
442 OM_uint32
443 gssEapValidateMechs(OM_uint32 *minor,
444                    const gss_OID_set mechs);
445
446 gss_buffer_t
447 gssEapOidToSaslName(const gss_OID oid);
448
449 gss_OID
450 gssEapSaslNameToOid(const gss_buffer_t name);
451
452 /* util_name.c */
453 #define EXPORT_NAME_FLAG_OID        0x1
454 #define EXPORT_NAME_FLAG_COMPOSITE  0x2
455
456 OM_uint32 gssEapAllocName(OM_uint32 *minor, gss_name_t *pName);
457 OM_uint32 gssEapReleaseName(OM_uint32 *minor, gss_name_t *pName);
458 OM_uint32 gssEapExportName(OM_uint32 *minor,
459                            const gss_name_t name,
460                            gss_buffer_t exportedName);
461 OM_uint32 gssEapExportNameInternal(OM_uint32 *minor,
462                                    const gss_name_t name,
463                                    gss_buffer_t exportedName,
464                                    unsigned int flags);
465 OM_uint32 gssEapImportName(OM_uint32 *minor,
466                            const gss_buffer_t input_name_buffer,
467                            gss_OID input_name_type,
468                            gss_name_t *output_name);
469 OM_uint32 gssEapImportNameInternal(OM_uint32 *minor,
470                                    const gss_buffer_t input_name_buffer,
471                                    gss_name_t *output_name,
472                                    unsigned int flags);
473 OM_uint32
474 gssEapDuplicateName(OM_uint32 *minor,
475                     const gss_name_t input_name,
476                     gss_name_t *dest_name);
477
478 /* util_oid.c */
479 OM_uint32
480 composeOid(OM_uint32 *minor_status,
481            const char *prefix,
482            size_t prefix_len,
483            int suffix,
484            gss_OID_desc *oid);
485
486 OM_uint32
487 decomposeOid(OM_uint32 *minor_status,
488              const char *prefix,
489              size_t prefix_len,
490              gss_OID_desc *oid,
491              int *suffix) ;
492
493 OM_uint32
494 duplicateOid(OM_uint32 *minor_status,
495              const gss_OID_desc * const oid,
496              gss_OID *new_oid);
497
498 OM_uint32
499 duplicateOidSet(OM_uint32 *minor,
500                 const gss_OID_set src,
501                 gss_OID_set *dst);
502
503 static inline int
504 oidEqual(const gss_OID_desc *o1, const gss_OID_desc *o2)
505 {
506     if (o1 == GSS_C_NO_OID)
507         return (o2 == GSS_C_NO_OID);
508     else if (o2 == GSS_C_NO_OID)
509         return (o1 == GSS_C_NO_OID);
510     else
511         return (o1->length == o2->length &&
512                 memcmp(o1->elements, o2->elements, o1->length) == 0);
513 }
514
515 /* util_ordering.c */
516 OM_uint32
517 sequenceInternalize(OM_uint32 *minor,
518                     void **vqueue,
519                     unsigned char **buf,
520                     size_t *lenremain);
521
522 OM_uint32
523 sequenceExternalize(OM_uint32 *minor,
524                     void *vqueue,
525                     unsigned char **buf,
526                     size_t *lenremain);
527
528 size_t
529 sequenceSize(void *vqueue);
530
531 OM_uint32
532 sequenceFree(OM_uint32 *minor, void **vqueue);
533
534 OM_uint32
535 sequenceCheck(OM_uint32 *minor, void **vqueue, uint64_t seqnum);
536
537 OM_uint32
538 sequenceInit(OM_uint32 *minor, void **vqueue, uint64_t seqnum,
539              int do_replay, int do_sequence, int wide_nums);
540
541 /* util_sm.c */
542 enum gss_eap_state {
543     GSSEAP_STATE_INITIAL        = 0x01,     /* initial state */
544     GSSEAP_STATE_AUTHENTICATE   = 0x02,     /* exchange EAP messages */
545     GSSEAP_STATE_INITIATOR_EXTS = 0x04,     /* initiator extensions */
546     GSSEAP_STATE_ACCEPTOR_EXTS  = 0x08,     /* acceptor extensions */
547 #ifdef GSSEAP_ENABLE_REAUTH
548     GSSEAP_STATE_REAUTHENTICATE = 0x10,     /* GSS reauthentication messages */
549 #endif
550     GSSEAP_STATE_ESTABLISHED    = 0x20,     /* context established */
551     GSSEAP_STATE_ALL            = 0x3F
552 };
553
554 #define GSSEAP_STATE_NEXT(s)    ((s) << 1)
555
556 #define GSSEAP_SM_STATE(ctx)                ((ctx)->state)
557
558 #ifdef GSSEAP_DEBUG
559 void gssEapSmTransition(gss_ctx_id_t ctx, enum gss_eap_state state);
560 #define GSSEAP_SM_TRANSITION(ctx, state)    gssEapSmTransition((ctx), (state))
561 #else
562 #define GSSEAP_SM_TRANSITION(ctx, newstate)    do { (ctx)->state = (newstate); } while (0)
563 #endif
564
565 #define GSSEAP_SM_TRANSITION_NEXT(ctx)      GSSEAP_SM_TRANSITION((ctx), GSSEAP_STATE_NEXT(GSSEAP_SM_STATE((ctx))))
566
567 /* state machine entry */
568 struct gss_eap_sm {
569     OM_uint32 inputTokenType;
570     OM_uint32 outputTokenType;
571     enum gss_eap_state validStates;
572     OM_uint32 itokFlags;
573     OM_uint32 (*processToken)(OM_uint32 *,
574                               gss_cred_id_t,
575                               gss_ctx_id_t,
576                               gss_name_t,
577                               gss_OID,
578                               OM_uint32,
579                               OM_uint32,
580                               gss_channel_bindings_t,
581                               gss_buffer_t,
582                               gss_buffer_t,
583                               OM_uint32 *);
584 };
585
586 /* state machine flags, set by handler */
587 #define SM_FLAG_FORCE_SEND_TOKEN            0x00000001  /* send token even if no inner tokens */
588 #define SM_FLAG_OUTPUT_TOKEN_CRITICAL       0x00000002  /* output token is critical */
589
590 /* state machine flags, set by state machine */
591 #define SM_FLAG_INPUT_TOKEN_CRITICAL        0x10000000  /* input token was critical */
592
593 #define SM_ITOK_FLAG_REQUIRED               0x00000001  /* received tokens must be present */
594
595 OM_uint32
596 gssEapSmStep(OM_uint32 *minor,
597              gss_cred_id_t cred,
598              gss_ctx_id_t ctx,
599              gss_name_t target,
600              gss_OID mech,
601              OM_uint32 reqFlags,
602              OM_uint32 timeReq,
603              gss_channel_bindings_t chanBindings,
604              gss_buffer_t inputToken,
605              gss_buffer_t outputToken,
606              struct gss_eap_sm *sm,
607              size_t smCount);
608
609 void
610 gssEapSmTransition(gss_ctx_id_t ctx, enum gss_eap_state state);
611
612 /* util_token.c */
613 OM_uint32
614 gssEapEncodeInnerTokens(OM_uint32 *minor,
615                         gss_buffer_set_t extensions,
616                         OM_uint32 *types,
617                         gss_buffer_t buffer);
618 OM_uint32
619 gssEapDecodeInnerTokens(OM_uint32 *minor,
620                         const gss_buffer_t buffer,
621                         gss_buffer_set_t *pExtensions,
622                         OM_uint32 **pTypes);
623
624 size_t
625 tokenSize(const gss_OID_desc *mech, size_t body_size);
626
627 void
628 makeTokenHeader(const gss_OID_desc *mech,
629                 size_t body_size,
630                 unsigned char **buf,
631                 enum gss_eap_token_type tok_type);
632
633 OM_uint32
634 verifyTokenHeader(OM_uint32 *minor,
635                   gss_OID mech,
636                   size_t *body_size,
637                   unsigned char **buf_in,
638                   size_t toksize_in,
639                   enum gss_eap_token_type *ret_tok_type);
640
641 /* Helper macros */
642
643 #define GSSEAP_CALLOC                   calloc
644 #define GSSEAP_MALLOC                   malloc
645 #define GSSEAP_FREE                     free
646 #define GSSEAP_REALLOC                  realloc
647
648 #define GSSEAP_NOT_IMPLEMENTED          do {            \
649         assert(0 && "not implemented");                 \
650         *minor = ENOSYS;                                \
651         return GSS_S_FAILURE;                           \
652     } while (0)
653
654 #include <pthread.h>
655
656 #define GSSEAP_MUTEX                    pthread_mutex_t
657 #define GSSEAP_MUTEX_INITIALIZER        PTHREAD_MUTEX_INITIALIZER
658
659 #define GSSEAP_MUTEX_INIT(m)            pthread_mutex_init((m), NULL)
660 #define GSSEAP_MUTEX_DESTROY(m)         pthread_mutex_destroy((m))
661 #define GSSEAP_MUTEX_LOCK(m)            pthread_mutex_lock((m))
662 #define GSSEAP_MUTEX_UNLOCK(m)          pthread_mutex_unlock((m))
663
664 #define GSSEAP_THREAD_KEY               pthread_key_t
665 #define GSSEAP_KEY_CREATE(k, d)         pthread_key_create((k), (d))
666 #define GSSEAP_GETSPECIFIC(k)           pthread_getspecific((k))
667 #define GSSEAP_SETSPECIFIC(k, d)        pthread_setspecific((k), (d))
668
669 #define GSSEAP_THREAD_ONCE              pthread_once_t
670 #define GSSEAP_ONCE(o, i)               pthread_once((o), (i))
671 #define GSSEAP_ONCE_INITIALIZER         PTHREAD_ONCE_INIT
672
673 /* Helper functions */
674 static inline void
675 store_uint16_be(uint16_t val, void *vp)
676 {
677     unsigned char *p = (unsigned char *)vp;
678
679     p[0] = (val >>  8) & 0xff;
680     p[1] = (val      ) & 0xff;
681 }
682
683 static inline uint16_t
684 load_uint16_be(const void *cvp)
685 {
686     const unsigned char *p = (const unsigned char *)cvp;
687
688     return (p[1] | (p[0] << 8));
689 }
690
691 static inline void
692 store_uint32_be(uint32_t val, void *vp)
693 {
694     unsigned char *p = (unsigned char *)vp;
695
696     p[0] = (val >> 24) & 0xff;
697     p[1] = (val >> 16) & 0xff;
698     p[2] = (val >>  8) & 0xff;
699     p[3] = (val      ) & 0xff;
700 }
701
702 static inline uint32_t
703 load_uint32_be(const void *cvp)
704 {
705     const unsigned char *p = (const unsigned char *)cvp;
706
707     return (p[3] | (p[2] << 8)
708             | ((uint32_t) p[1] << 16)
709             | ((uint32_t) p[0] << 24));
710 }
711
712 static inline void
713 store_uint64_be(uint64_t val, void *vp)
714 {
715     unsigned char *p = (unsigned char *)vp;
716
717     p[0] = (unsigned char)((val >> 56) & 0xff);
718     p[1] = (unsigned char)((val >> 48) & 0xff);
719     p[2] = (unsigned char)((val >> 40) & 0xff);
720     p[3] = (unsigned char)((val >> 32) & 0xff);
721     p[4] = (unsigned char)((val >> 24) & 0xff);
722     p[5] = (unsigned char)((val >> 16) & 0xff);
723     p[6] = (unsigned char)((val >>  8) & 0xff);
724     p[7] = (unsigned char)((val      ) & 0xff);
725 }
726
727 static inline uint64_t
728 load_uint64_be(const void *cvp)
729 {
730     const unsigned char *p = (const unsigned char *)cvp;
731
732     return ((uint64_t)load_uint32_be(p) << 32) | load_uint32_be(p + 4);
733 }
734
735 static inline unsigned char *
736 store_buffer(gss_buffer_t buffer, void *vp, int wide_nums)
737 {
738     unsigned char *p = (unsigned char *)vp;
739
740     if (wide_nums) {
741         store_uint64_be(buffer->length, p);
742         p += 8;
743     } else {
744         store_uint32_be(buffer->length, p);
745         p += 4;
746     }
747
748     if (buffer->value != NULL) {
749         memcpy(p, buffer->value, buffer->length);
750         p += buffer->length;
751     }
752
753     return p;
754 }
755
756 static inline unsigned char *
757 load_buffer(const void *cvp, size_t length, gss_buffer_t buffer)
758 {
759     buffer->length = 0;
760     buffer->value = GSSEAP_MALLOC(length);
761     if (buffer->value == NULL)
762         return NULL;
763     buffer->length = length;
764     memcpy(buffer->value, cvp, length);
765     return (unsigned char *)cvp + length;
766 }
767
768 static inline unsigned char *
769 store_oid(gss_OID oid, void *vp)
770 {
771     gss_buffer_desc buf;
772
773     if (oid != GSS_C_NO_OID) {
774         buf.length = oid->length;
775         buf.value = oid->elements;
776     } else {
777         buf.length = 0;
778         buf.value = NULL;
779     }
780
781     return store_buffer(&buf, vp, FALSE);
782 }
783
784 static inline void
785 krbDataToGssBuffer(krb5_data *data, gss_buffer_t buffer)
786 {
787     buffer->value = (void *)data->data;
788     buffer->length = data->length;
789 }
790
791 static inline void
792 krbPrincComponentToGssBuffer(krb5_principal krbPrinc,
793                              int index, gss_buffer_t buffer)
794 {
795 #ifdef HAVE_HEIMDAL_VERSION
796     buffer->value = (void *)KRB_PRINC_NAME(krbPrinc)[index];
797     buffer->length = strlen((char *)buffer->value);
798 #else
799     buffer->value = (void *)krb5_princ_component(NULL, krbPrinc, index)->data;
800     buffer->length = krb5_princ_component(NULL, krbPrinc, index)->length;
801 #endif /* HAVE_HEIMDAL_VERSION */
802 }
803
804 static inline void
805 krbPrincRealmToGssBuffer(krb5_principal krbPrinc, gss_buffer_t buffer)
806 {
807 #ifdef HAVE_HEIMDAL_VERSION
808     buffer->value = (void *)KRB_PRINC_REALM(krbPrinc);
809     buffer->length = strlen((char *)buffer->value);
810 #else
811     krbDataToGssBuffer(KRB_PRINC_REALM(krbPrinc), buffer);
812 #endif
813 }
814
815 static inline void
816 gssBufferToKrbData(gss_buffer_t buffer, krb5_data *data)
817 {
818     data->data = (char *)buffer->value;
819     data->length = buffer->length;
820 }
821
822 #ifdef __cplusplus
823 }
824 #endif
825
826 #include "util_attr.h"
827 #ifdef GSSEAP_ENABLE_REAUTH
828 #include "util_reauth.h"
829 #endif
830
831 #endif /* _UTIL_H_ */