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