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