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