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