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