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