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