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