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