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