Updated to hostap_2_6
[mech_eap.git] / mech_eap / util.h
1 /*
2  * Copyright (c) 2011, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of JANET(UK) nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 /*
33  * Portions Copyright 2003-2010 Massachusetts Institute of Technology.
34  * All Rights Reserved.
35  *
36  * Export of this software from the United States of America may
37  *   require a specific license from the United States Government.
38  *   It is the responsibility of any person or organization contemplating
39  *   export to obtain such a license before exporting.
40  *
41  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
42  * distribute this software and its documentation for any purpose and
43  * without fee is hereby granted, provided that the above copyright
44  * notice appear in all copies and that both that copyright notice and
45  * this permission notice appear in supporting documentation, and that
46  * the name of M.I.T. not be used in advertising or publicity pertaining
47  * to distribution of the software without specific, written prior
48  * permission.  Furthermore if you modify this software you must label
49  * your software as modified software and not distribute it in such a
50  * fashion that it might be confused with the original M.I.T. software.
51  * M.I.T. makes no representations about the suitability of
52  * this software for any purpose.  It is provided "as is" without express
53  * or implied warranty.
54  *
55  */
56
57 /*
58  * Utility functions.
59  */
60
61 #ifndef _UTIL_H_
62 #define _UTIL_H_ 1
63
64 #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 # ifndef __cplusplus
77 # define inline __inline
78 # endif
79 #define snprintf _snprintf
80 #endif
81
82 #ifdef __cplusplus
83 extern "C" {
84 #endif
85
86 #ifndef MIN
87 #define MIN(_a,_b)  ((_a)<(_b)?(_a):(_b))
88 #endif
89
90 #if !defined(WIN32) && !(defined(__cplusplus)) || (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))
91 #define GSSEAP_UNUSED __attribute__ ((__unused__))
92 #else
93 #define GSSEAP_UNUSED
94 #endif
95
96 /* util_buffer.c */
97 OM_uint32
98 makeStringBuffer(OM_uint32 *minor,
99                  const char *string,
100                  gss_buffer_t buffer);
101
102 #define makeStringBufferOrCleanup(src, dst)             \
103     do {                                                \
104         major = makeStringBuffer((minor), (src), (dst));\
105         if (GSS_ERROR(major))                           \
106             goto cleanup;                               \
107     } while (0)
108
109 OM_uint32
110 bufferToString(OM_uint32 *minor,
111                const gss_buffer_t buffer,
112                char **pString);
113
114 OM_uint32
115 duplicateBuffer(OM_uint32 *minor,
116                 const gss_buffer_t src,
117                 gss_buffer_t dst);
118
119 #define duplicateBufferOrCleanup(src, dst)              \
120     do {                                                \
121         major = duplicateBuffer((minor), (src), (dst)); \
122         if (GSS_ERROR(major))                           \
123             goto cleanup;                               \
124     } while (0)
125
126 static inline int
127 bufferEqual(const gss_buffer_t b1, const gss_buffer_t b2)
128 {
129     return (b1->length == b2->length &&
130             memcmp(b1->value, b2->value, b2->length) == 0);
131 }
132
133 static inline int
134 bufferEqualString(const gss_buffer_t b1, const char *s)
135 {
136     gss_buffer_desc b2;
137
138     b2.length = strlen(s);
139     b2.value = (char *)s;
140
141     return bufferEqual(b1, &b2);
142 }
143
144 /* util_cksum.c */
145 int
146 gssEapSign(krb5_context context,
147            krb5_cksumtype type,
148            size_t rrc,
149 #ifdef HAVE_HEIMDAL_VERSION
150            krb5_crypto crypto,
151 #else
152            krb5_keyblock *key,
153 #endif
154            krb5_keyusage sign_usage,
155            gss_iov_buffer_desc *iov,
156            int iov_count);
157
158 int
159 gssEapVerify(krb5_context context,
160              krb5_cksumtype type,
161              size_t rrc,
162 #ifdef HAVE_HEIMDAL_VERSION
163              krb5_crypto crypto,
164 #else
165              krb5_keyblock *key,
166 #endif
167              krb5_keyusage sign_usage,
168              gss_iov_buffer_desc *iov,
169              int iov_count,
170              int *valid);
171
172 #if 0
173 OM_uint32
174 gssEapEncodeGssChannelBindings(OM_uint32 *minor,
175                                gss_channel_bindings_t chanBindings,
176                                gss_buffer_t encodedBindings);
177 #endif
178
179 /* util_context.c */
180 #define EAP_EXPORT_CONTEXT_V1           1
181
182 enum gss_eap_token_type {
183     TOK_TYPE_NONE                    = 0x0000,  /* no token */
184     TOK_TYPE_MIC                     = 0x0404,  /* RFC 4121 MIC token */
185     TOK_TYPE_WRAP                    = 0x0504,  /* RFC 4121 wrap token */
186     TOK_TYPE_EXPORT_NAME             = 0x0401,  /* RFC 2743 exported name */
187     TOK_TYPE_EXPORT_NAME_COMPOSITE   = 0x0402,  /* exported composite name */
188     TOK_TYPE_DELETE_CONTEXT          = 0x0405,  /* RFC 2743 delete context */
189     TOK_TYPE_INITIATOR_CONTEXT       = 0x0601,  /* initiator-sent context token */
190     TOK_TYPE_ACCEPTOR_CONTEXT        = 0x0602,  /* acceptor-sent context token */
191 };
192
193 /* inner token types and flags */
194 #define ITOK_TYPE_NONE                  0x00000000
195 #define ITOK_TYPE_CONTEXT_ERR           0x00000001 /* critical */
196 #define ITOK_TYPE_ACCEPTOR_NAME_REQ     0x00000002 /* TBD */
197 #define ITOK_TYPE_ACCEPTOR_NAME_RESP    0x00000003 /* TBD */
198 #define ITOK_TYPE_EAP_RESP              0x00000004 /* critical, required, if not reauth */
199 #define ITOK_TYPE_EAP_REQ               0x00000005 /* critical, required, if not reauth */
200 #define ITOK_TYPE_GSS_CHANNEL_BINDINGS  0x00000006 /* critical, required, if not reauth */
201 #define ITOK_TYPE_REAUTH_CREDS          0x00000007 /* optional */
202 #define ITOK_TYPE_REAUTH_REQ            0x00000008 /* optional */
203 #define ITOK_TYPE_REAUTH_RESP           0x00000009 /* optional */
204 #define ITOK_TYPE_VERSION_INFO          0x0000000A /* optional */
205 #define ITOK_TYPE_VENDOR_INFO           0x0000000B /* optional */
206 #define ITOK_TYPE_GSS_FLAGS             0x0000000C /* optional */
207 #define ITOK_TYPE_INITIATOR_MIC         0x0000000D /* critical, required, if not reauth */
208 #define ITOK_TYPE_ACCEPTOR_MIC          0x0000000E /* TBD */
209
210 #define ITOK_FLAG_CRITICAL              0x80000000  /* critical, wire flag */
211 #define ITOK_FLAG_VERIFIED              0x40000000  /* verified, API flag */
212
213 #define ITOK_TYPE_MASK                  (~(ITOK_FLAG_CRITICAL | ITOK_FLAG_VERIFIED))
214
215 #define GSSEAP_WIRE_FLAGS_MASK          ( GSS_C_MUTUAL_FLAG             | \
216                                           GSS_C_DCE_STYLE               | \
217                                           GSS_C_IDENTIFY_FLAG           | \
218                                           GSS_C_EXTENDED_ERROR_FLAG       )
219
220 OM_uint32 gssEapAllocContext(OM_uint32 *minor, gss_ctx_id_t *pCtx);
221 OM_uint32 gssEapReleaseContext(OM_uint32 *minor, gss_ctx_id_t *pCtx);
222
223 OM_uint32
224 gssEapMakeToken(OM_uint32 *minor,
225                 gss_ctx_id_t ctx,
226                 const gss_buffer_t innerToken,
227                 enum gss_eap_token_type tokenType,
228                 gss_buffer_t outputToken);
229
230 OM_uint32
231 gssEapVerifyToken(OM_uint32 *minor,
232                   gss_ctx_id_t ctx,
233                   const gss_buffer_t inputToken,
234                   enum gss_eap_token_type *tokenType,
235                   gss_buffer_t innerInputToken);
236
237 OM_uint32
238 gssEapContextTime(OM_uint32 *minor,
239                   gss_ctx_id_t context_handle,
240                   OM_uint32 *time_rec);
241
242 OM_uint32
243 gssEapMakeTokenMIC(OM_uint32 *minor,
244                    gss_ctx_id_t ctx,
245                    gss_buffer_t tokenMIC);
246
247 OM_uint32
248 gssEapVerifyTokenMIC(OM_uint32 *minor,
249                      gss_ctx_id_t ctx,
250                      const gss_buffer_t tokenMIC);
251
252 /* util_cred.c */
253 OM_uint32 gssEapAllocCred(OM_uint32 *minor, gss_cred_id_t *pCred);
254 OM_uint32 gssEapReleaseCred(OM_uint32 *minor, gss_cred_id_t *pCred);
255
256 gss_OID
257 gssEapPrimaryMechForCred(gss_cred_id_t cred);
258
259 OM_uint32
260 gssEapAcquireCred(OM_uint32 *minor,
261                   const gss_name_t desiredName,
262                   OM_uint32 timeReq,
263                   const gss_OID_set desiredMechs,
264                   int cred_usage,
265                   gss_cred_id_t *pCred,
266                   gss_OID_set *pActualMechs,
267                   OM_uint32 *timeRec);
268
269 OM_uint32
270 gssEapSetCredPassword(OM_uint32 *minor,
271                       gss_cred_id_t cred,
272                       const gss_buffer_t password);
273
274 OM_uint32
275 gssEapSetCredClientCertificate(OM_uint32 *minor,
276                                gss_cred_id_t cred,
277                                const gss_buffer_t clientCert,
278                                const gss_buffer_t privateKey);
279
280 OM_uint32
281 gssEapSetCredService(OM_uint32 *minor,
282                      gss_cred_id_t cred,
283                      const gss_name_t target);
284
285 OM_uint32
286 gssEapResolveInitiatorCred(OM_uint32 *minor,
287                            const gss_cred_id_t cred,
288                            const gss_name_t target,
289                            gss_cred_id_t *resolvedCred);
290
291 int gssEapCredAvailable(gss_cred_id_t cred, gss_OID mech);
292
293 OM_uint32
294 gssEapInquireCred(OM_uint32 *minor,
295                   gss_cred_id_t cred,
296                   gss_name_t *name,
297                   OM_uint32 *pLifetime,
298                   gss_cred_usage_t *cred_usage,
299                   gss_OID_set *mechanisms);
300
301 /* util_crypt.c */
302 int
303 gssEapEncrypt(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 gssEapDecrypt(krb5_context context, int dce_style, size_t ec,
315               size_t rrc,
316 #ifdef HAVE_HEIMDAL_VERSION
317               krb5_crypto crypto,
318 #else
319               krb5_keyblock *key,
320 #endif
321               int usage,
322               gss_iov_buffer_desc *iov, int iov_count);
323
324 int
325 gssEapMapCryptoFlag(OM_uint32 type);
326
327 gss_iov_buffer_t
328 gssEapLocateIov(gss_iov_buffer_desc *iov,
329                 int iov_count,
330                 OM_uint32 type);
331
332 void
333 gssEapIovMessageLength(gss_iov_buffer_desc *iov,
334                        int iov_count,
335                        size_t *data_length,
336                        size_t *assoc_data_length);
337
338 void
339 gssEapReleaseIov(gss_iov_buffer_desc *iov, int iov_count);
340
341 int
342 gssEapIsIntegrityOnly(gss_iov_buffer_desc *iov, int iov_count);
343
344 int
345 gssEapAllocIov(gss_iov_buffer_t iov, size_t size);
346
347 OM_uint32
348 gssEapDeriveRfc3961Key(OM_uint32 *minor,
349                        const unsigned char *key,
350                        size_t keyLength,
351                        krb5_enctype enctype,
352                        krb5_keyblock *pKey);
353
354 /* util_krb.c */
355
356 #ifndef KRB_MALLOC
357 /*
358  * If your Kerberos library uses a different allocator to your
359  * GSS mechanism glue, then you might wish to define these in
360  * config.h or elsewhere. This should eventually go away when
361  * we no longer need to allocate memory that is freed by the
362  * Kerberos library.
363  */
364 #define KRB_CALLOC                      calloc
365 #define KRB_MALLOC                      malloc
366 #define KRB_FREE                        free
367 #define KRB_REALLOC                     realloc
368 #endif /* KRB_MALLOC */
369
370 #ifdef HAVE_HEIMDAL_VERSION
371
372 #define KRB_TIME_FOREVER        ((time_t)~0L)
373
374 #define KRB_KEY_TYPE(key)       ((key)->keytype)
375 #define KRB_KEY_DATA(key)       ((key)->keyvalue.data)
376 #define KRB_KEY_LENGTH(key)     ((key)->keyvalue.length)
377
378 #define KRB_PRINC_LENGTH(princ) ((princ)->name.name_string.len)
379 #define KRB_PRINC_TYPE(princ)   ((princ)->name.name_type)
380 #define KRB_PRINC_NAME(princ)   ((princ)->name.name_string.val)
381 #define KRB_PRINC_REALM(princ)  ((princ)->realm)
382
383 #define KRB_KT_ENT_KEYBLOCK(e)  (&(e)->keyblock)
384 #define KRB_KT_ENT_FREE(c, e)   krb5_kt_free_entry((c), (e))
385
386 #define KRB_CRYPTO_CONTEXT(ctx) (krbCrypto)
387
388 #define KRB_DATA_INIT(d)        krb5_data_zero((d))
389
390 #define KRB_CHECKSUM_TYPE(c)    ((c)->cksumtype)
391 #define KRB_CHECKSUM_LENGTH(c)  ((c)->checksum.length)
392 #define KRB_CHECKSUM_DATA(c)    ((c)->checksum.data)
393
394 #define KRB_CHECKSUM_INIT(cksum, type, d)      do { \
395         (cksum)->cksumtype = (type);                \
396         (cksum)->checksum.length = (d)->length;     \
397         (cksum)->checksum.data = (d)->value;        \
398     } while (0)
399
400 #else
401
402 #define KRB_TIME_FOREVER        KRB5_INT32_MAX
403
404 #define KRB_KEY_TYPE(key)       ((key)->enctype)
405 #define KRB_KEY_DATA(key)       ((key)->contents)
406 #define KRB_KEY_LENGTH(key)     ((key)->length)
407
408 #define KRB_PRINC_LENGTH(princ) (krb5_princ_size(NULL, (princ)))
409 #define KRB_PRINC_TYPE(princ)   (krb5_princ_type(NULL, (princ)))
410 #define KRB_PRINC_NAME(princ)   (krb5_princ_name(NULL, (princ)))
411 #define KRB_PRINC_REALM(princ)  (krb5_princ_realm(NULL, (princ)))
412 #define KRB_PRINC_COMPONENT(princ, component) \
413         (krb5_princ_component(NULL, (princ), (component)))
414
415 #define KRB_KT_ENT_KEYBLOCK(e)  (&(e)->key)
416 #define KRB_KT_ENT_FREE(c, e)   krb5_free_keytab_entry_contents((c), (e))
417
418 #define KRB_CRYPTO_CONTEXT(ctx) (&(ctx)->rfc3961Key)
419
420 #define KRB_DATA_INIT(d)        do {        \
421         (d)->magic = KV5M_DATA;             \
422         (d)->length = 0;                    \
423         (d)->data = NULL;                   \
424     } while (0)
425
426 #define KRB_CHECKSUM_TYPE(c)    ((c)->checksum_type)
427 #define KRB_CHECKSUM_LENGTH(c)  ((c)->length)
428 #define KRB_CHECKSUM_DATA(c)    ((c)->contents)
429
430 #define KRB_CHECKSUM_INIT(cksum, type, d)      do { \
431         (cksum)->checksum_type = (type);            \
432         (cksum)->length = (d)->length;              \
433         (cksum)->contents = (d)->value;             \
434     } while (0)
435
436 #endif /* HAVE_HEIMDAL_VERSION */
437
438 #define KRB_KEY_INIT(key)       do {        \
439         KRB_KEY_TYPE(key) = ENCTYPE_NULL;   \
440         KRB_KEY_DATA(key) = NULL;           \
441         KRB_KEY_LENGTH(key) = 0;            \
442     } while (0)
443
444 #define GSSEAP_KRB_INIT(ctx) do {                   \
445         OM_uint32 tmpMajor;                         \
446                                                     \
447         tmpMajor  = gssEapKerberosInit(minor, ctx); \
448         if (GSS_ERROR(tmpMajor)) {                  \
449             return tmpMajor;                        \
450         }                                           \
451     } while (0)
452
453 OM_uint32
454 gssEapKerberosInit(OM_uint32 *minor, krb5_context *context);
455
456 OM_uint32
457 rfc3961ChecksumTypeForKey(OM_uint32 *minor,
458                           krb5_keyblock *key,
459                           krb5_cksumtype *cksumtype);
460
461 krb5_error_code
462 krbCryptoLength(krb5_context krbContext,
463 #ifdef HAVE_HEIMDAL_VERSION
464                 krb5_crypto krbCrypto,
465 #else
466                 krb5_keyblock *key,
467 #endif
468                 int type,
469                 size_t *length);
470
471 krb5_error_code
472 krbPaddingLength(krb5_context krbContext,
473 #ifdef HAVE_HEIMDAL_VERSION
474                  krb5_crypto krbCrypto,
475 #else
476                  krb5_keyblock *key,
477 #endif
478                  size_t dataLength,
479                  size_t *padLength);
480
481 krb5_error_code
482 krbBlockSize(krb5_context krbContext,
483 #ifdef HAVE_HEIMDAL_VERSION
484                  krb5_crypto krbCrypto,
485 #else
486                  krb5_keyblock *key,
487 #endif
488                  size_t *blockSize);
489
490 krb5_error_code
491 krbEnctypeToString(krb5_context krbContext,
492                    krb5_enctype enctype,
493                    const char *prefix,
494                    gss_buffer_t string);
495
496 krb5_error_code
497 krbMakeAuthDataKdcIssued(krb5_context context,
498                          const krb5_keyblock *key,
499                          krb5_const_principal issuer,
500 #ifdef HAVE_HEIMDAL_VERSION
501                          const AuthorizationData *authdata,
502                          AuthorizationData *adKdcIssued
503 #else
504                          krb5_authdata *const *authdata,
505                          krb5_authdata ***adKdcIssued
506 #endif
507                          );
508
509 krb5_error_code
510 krbMakeCred(krb5_context context,
511             krb5_auth_context authcontext,
512             krb5_creds *creds,
513             krb5_data *data);
514
515 /* util_lucid.c */
516 OM_uint32
517 gssEapExportLucidSecContext(OM_uint32 *minor,
518                             gss_ctx_id_t ctx,
519                             const gss_OID desiredObject,
520                             gss_buffer_set_t *data_set);
521
522 /* util_mech.c */
523 extern gss_OID GSS_EAP_MECHANISM;
524
525 #define OID_FLAG_NULL_VALID                 0x00000001
526 #define OID_FLAG_FAMILY_MECH_VALID          0x00000002
527 #define OID_FLAG_MAP_NULL_TO_DEFAULT_MECH   0x00000004
528 #define OID_FLAG_MAP_FAMILY_MECH_TO_NULL    0x00000008
529
530 OM_uint32
531 gssEapCanonicalizeOid(OM_uint32 *minor,
532                       const gss_OID oid,
533                       OM_uint32 flags,
534                       gss_OID *pOid);
535
536 OM_uint32
537 gssEapReleaseOid(OM_uint32 *minor, gss_OID *oid);
538
539 OM_uint32
540 gssEapDefaultMech(OM_uint32 *minor,
541                   gss_OID *oid);
542
543 OM_uint32
544 gssEapIndicateMechs(OM_uint32 *minor,
545                     gss_OID_set *mechs);
546
547 OM_uint32
548 gssEapEnctypeToOid(OM_uint32 *minor,
549                    krb5_enctype enctype,
550                    gss_OID *pOid);
551
552 OM_uint32
553 gssEapOidToEnctype(OM_uint32 *minor,
554                    const gss_OID oid,
555                    krb5_enctype *enctype);
556
557 int
558 gssEapIsMechanismOid(const gss_OID oid);
559
560 int
561 gssEapIsConcreteMechanismOid(const gss_OID oid);
562
563 OM_uint32
564 gssEapValidateMechs(OM_uint32 *minor,
565                    const gss_OID_set mechs);
566
567 gss_buffer_t
568 gssEapOidToSaslName(const gss_OID oid);
569
570 gss_OID
571 gssEapSaslNameToOid(const gss_buffer_t name);
572
573 /* util_moonshot.c */
574 OM_uint32
575 libMoonshotResolveDefaultIdentity(OM_uint32 *minor,
576                                   const gss_cred_id_t cred,
577                                   gss_name_t *pName);
578
579 OM_uint32
580 libMoonshotResolveInitiatorCred(OM_uint32 *minor,
581                                 gss_cred_id_t cred,
582                                 const gss_name_t targetName);
583
584 /* util_name.c */
585 #define EXPORT_NAME_FLAG_OID                    0x1
586 #define EXPORT_NAME_FLAG_COMPOSITE              0x2
587 #define EXPORT_NAME_FLAG_ALLOW_COMPOSITE        0x4
588
589 OM_uint32 gssEapAllocName(OM_uint32 *minor, gss_name_t *pName);
590 OM_uint32 gssEapReleaseName(OM_uint32 *minor, gss_name_t *pName);
591 OM_uint32 gssEapExportName(OM_uint32 *minor,
592                            const gss_name_t name,
593                            gss_buffer_t exportedName);
594 OM_uint32 gssEapExportNameInternal(OM_uint32 *minor,
595                                    const gss_name_t name,
596                                    gss_buffer_t exportedName,
597                                    OM_uint32 flags);
598 OM_uint32 gssEapImportName(OM_uint32 *minor,
599                            const gss_buffer_t input_name_buffer,
600                            const gss_OID input_name_type,
601                            const gss_OID input_mech_type,
602                            gss_name_t *output_name);
603 OM_uint32 gssEapImportNameInternal(OM_uint32 *minor,
604                                    const gss_buffer_t input_name_buffer,
605                                    gss_name_t *output_name,
606                                    OM_uint32 flags);
607 OM_uint32
608 gssEapDuplicateName(OM_uint32 *minor,
609                     const gss_name_t input_name,
610                     gss_name_t *dest_name);
611
612 OM_uint32
613 gssEapCanonicalizeName(OM_uint32 *minor,
614                        const gss_name_t input_name,
615                        const gss_OID mech_type,
616                        gss_name_t *dest_name);
617
618 OM_uint32
619 gssEapDisplayName(OM_uint32 *minor,
620                   gss_name_t name,
621                   gss_buffer_t output_name_buffer,
622                   gss_OID *output_name_type);
623
624 #define COMPARE_NAME_FLAG_IGNORE_EMPTY_REALMS   0x1
625
626 OM_uint32
627 gssEapCompareName(OM_uint32 *minor,
628                   gss_name_t name1,
629                   gss_name_t name2,
630                   OM_uint32 flags,
631                   int *name_equal);
632
633 /* util_oid.c */
634 OM_uint32
635 composeOid(OM_uint32 *minor_status,
636            const char *prefix,
637            size_t prefix_len,
638            int suffix,
639            gss_OID_desc *oid);
640
641 OM_uint32
642 decomposeOid(OM_uint32 *minor_status,
643              const char *prefix,
644              size_t prefix_len,
645              gss_OID_desc *oid,
646              int *suffix) ;
647
648 OM_uint32
649 duplicateOid(OM_uint32 *minor_status,
650              const gss_OID_desc * const oid,
651              gss_OID *new_oid);
652
653 OM_uint32
654 duplicateOidSet(OM_uint32 *minor,
655                 const gss_OID_set src,
656                 gss_OID_set *dst);
657
658 extern int
659 oidEqual(const gss_OID_desc *o1, const gss_OID_desc *o2);
660
661 /* util_ordering.c */
662 OM_uint32
663 sequenceInternalize(OM_uint32 *minor,
664                     void **vqueue,
665                     unsigned char **buf,
666                     size_t *lenremain);
667
668 OM_uint32
669 sequenceExternalize(OM_uint32 *minor,
670                     void *vqueue,
671                     unsigned char **buf,
672                     size_t *lenremain);
673
674 size_t
675 sequenceSize(void *vqueue);
676
677 OM_uint32
678 sequenceFree(OM_uint32 *minor, void **vqueue);
679
680 OM_uint32
681 sequenceCheck(OM_uint32 *minor, void **vqueue, uint64_t seqnum);
682
683 OM_uint32
684 sequenceInit(OM_uint32 *minor, void **vqueue, uint64_t seqnum,
685              int do_replay, int do_sequence, int wide_nums);
686
687 /* util_sm.c */
688 enum gss_eap_state {
689     GSSEAP_STATE_INITIAL        = 0x01,     /* initial state */
690     GSSEAP_STATE_AUTHENTICATE   = 0x02,     /* exchange EAP messages */
691     GSSEAP_STATE_INITIATOR_EXTS = 0x04,     /* initiator extensions */
692     GSSEAP_STATE_ACCEPTOR_EXTS  = 0x08,     /* acceptor extensions */
693 #ifdef GSSEAP_ENABLE_REAUTH
694     GSSEAP_STATE_REAUTHENTICATE = 0x10,     /* GSS reauthentication messages */
695 #endif
696     GSSEAP_STATE_ESTABLISHED    = 0x20,     /* context established */
697     GSSEAP_STATE_ALL            = 0x3F
698 };
699
700 #define GSSEAP_STATE_NEXT(s)    ((s) << 1)
701
702 #define GSSEAP_SM_STATE(ctx)                ((ctx)->state)
703
704 #ifdef GSSEAP_DEBUG
705 void gssEapSmTransition(gss_ctx_id_t ctx, enum gss_eap_state state);
706 #define GSSEAP_SM_TRANSITION(ctx, state)    gssEapSmTransition((ctx), (state))
707 #else
708 #define GSSEAP_SM_TRANSITION(ctx, newstate)    do { (ctx)->state = (newstate); } while (0)
709 #endif
710
711 #define GSSEAP_SM_TRANSITION_NEXT(ctx)      GSSEAP_SM_TRANSITION((ctx), GSSEAP_STATE_NEXT(GSSEAP_SM_STATE((ctx))))
712
713 /* state machine entry */
714 struct gss_eap_sm {
715     OM_uint32 inputTokenType;
716     OM_uint32 outputTokenType;
717     enum gss_eap_state validStates;
718     OM_uint32 itokFlags;
719     OM_uint32 (*processToken)(OM_uint32 *,
720                               gss_cred_id_t,
721                               gss_ctx_id_t,
722                               gss_name_t,
723                               gss_OID,
724                               OM_uint32,
725                               OM_uint32,
726                               gss_channel_bindings_t,
727                               gss_buffer_t,
728                               gss_buffer_t,
729                               OM_uint32 *);
730 };
731
732 /* state machine flags, set by handler */
733 #define SM_FLAG_FORCE_SEND_TOKEN            0x00000001  /* send token even if no inner tokens */
734 #define SM_FLAG_OUTPUT_TOKEN_CRITICAL       0x00000002  /* output token is critical */
735
736 /* state machine flags, set by state machine */
737 #define SM_FLAG_INPUT_TOKEN_CRITICAL        0x10000000  /* input token was critical */
738
739 #define SM_ITOK_FLAG_REQUIRED               0x00000001  /* received tokens must be present */
740
741 OM_uint32
742 gssEapSmStep(OM_uint32 *minor,
743              gss_cred_id_t cred,
744              gss_ctx_id_t ctx,
745              gss_name_t target,
746              gss_OID mech,
747              OM_uint32 reqFlags,
748              OM_uint32 timeReq,
749              gss_channel_bindings_t chanBindings,
750              gss_buffer_t inputToken,
751              gss_buffer_t outputToken,
752              struct gss_eap_sm *sm,
753              size_t smCount);
754
755 void
756 gssEapSmTransition(gss_ctx_id_t ctx, enum gss_eap_state state);
757
758 /* util_token.c */
759 struct gss_eap_token_buffer_set {
760     gss_buffer_set_desc buffers; /* pointers only */
761     OM_uint32 *types;
762 };
763
764 OM_uint32
765 gssEapEncodeInnerTokens(OM_uint32 *minor,
766                         struct gss_eap_token_buffer_set *tokens,
767                         gss_buffer_t buffer);
768 OM_uint32
769 gssEapDecodeInnerTokens(OM_uint32 *minor,
770                         const gss_buffer_t buffer,
771                         struct gss_eap_token_buffer_set *tokens);
772
773 OM_uint32
774 gssEapReleaseInnerTokens(OM_uint32 *minor,
775                          struct gss_eap_token_buffer_set *tokens,
776                          int freeBuffers);
777
778 OM_uint32
779 gssEapAllocInnerTokens(OM_uint32 *minor,
780                        size_t count,
781                        struct gss_eap_token_buffer_set *tokens);
782
783 size_t
784 tokenSize(const gss_OID_desc *mech, size_t body_size);
785
786 void
787 makeTokenHeader(const gss_OID_desc *mech,
788                 size_t body_size,
789                 unsigned char **buf,
790                 enum gss_eap_token_type tok_type);
791
792 OM_uint32
793 verifyTokenHeader(OM_uint32 *minor,
794                   gss_OID mech,
795                   size_t *body_size,
796                   unsigned char **buf_in,
797                   size_t toksize_in,
798                   enum gss_eap_token_type *ret_tok_type);
799
800 /* Helper macros */
801 #ifndef GSSEAP_MALLOC
802 #if _WIN32
803 #include <gssapi/gssapi_alloc.h>
804 #define GSSEAP_MALLOC                   gssalloc_malloc
805 #define GSSEAP_CALLOC                   gssalloc_calloc
806 #define GSSEAP_FREE                     gssalloc_free
807 #define GSSEAP_REALLOC                  gssalloc_realloc
808 #else
809 #define GSSEAP_CALLOC                   calloc
810 #define GSSEAP_MALLOC                   malloc
811 #define GSSEAP_FREE                     free
812 #define GSSEAP_REALLOC                  realloc
813 #endif /* _WIN32 */
814 #endif /* !GSSEAP_MALLOC */
815
816 #ifndef GSSAPI_CALLCONV
817 #define GSSAPI_CALLCONV                 KRB5_CALLCONV
818 #endif
819
820 #ifndef GSSEAP_ASSERT
821 #include <assert.h>
822 #define GSSEAP_ASSERT(x)                assert((x))
823 #endif /* !GSSEAP_ASSERT */
824
825 #ifdef WIN32
826 #define GSSEAP_CONSTRUCTOR
827 #define GSSEAP_DESTRUCTOR
828 #else
829 #define GSSEAP_CONSTRUCTOR              __attribute__((constructor))
830 #define GSSEAP_DESTRUCTOR               __attribute__((destructor))
831 #endif
832
833 #define GSSEAP_NOT_IMPLEMENTED          do {            \
834         GSSEAP_ASSERT(0 && "not implemented");          \
835         *minor = ENOSYS;                                \
836         return GSS_S_FAILURE;                           \
837     } while (0)
838
839 #ifdef WIN32
840
841 #include <winbase.h>
842
843 #define GSSEAP_GET_LAST_ERROR()         (GetLastError()) /* XXX FIXME */
844
845 #define GSSEAP_MUTEX                    CRITICAL_SECTION
846 #define GSSEAP_MUTEX_INIT(m)            (InitializeCriticalSection((m)), 0)
847 #define GSSEAP_MUTEX_DESTROY(m)         DeleteCriticalSection((m))
848 #define GSSEAP_MUTEX_LOCK(m)            EnterCriticalSection((m))
849 #define GSSEAP_MUTEX_UNLOCK(m)          LeaveCriticalSection((m))
850 #define GSSEAP_ONCE_LEAVE               do { return TRUE; } while (0)
851
852 /* Thread-local is handled separately */
853
854 #define GSSEAP_THREAD_ONCE              INIT_ONCE
855 #define GSSEAP_ONCE_CALLBACK(cb)        BOOL CALLBACK cb(PINIT_ONCE InitOnce, PVOID Parameter, PVOID *Context)
856 #define GSSEAP_ONCE(o, i)               InitOnceExecuteOnce((o), (i), NULL, NULL)
857 #define GSSEAP_ONCE_INITIALIZER         INIT_ONCE_STATIC_INIT
858
859 #else
860
861 #include <pthread.h>
862
863 #define GSSEAP_GET_LAST_ERROR()         (errno)
864
865 #define GSSEAP_MUTEX                    pthread_mutex_t
866 #define GSSEAP_MUTEX_INIT(m)            pthread_mutex_init((m), NULL)
867 #define GSSEAP_MUTEX_DESTROY(m)         pthread_mutex_destroy((m))
868 #define GSSEAP_MUTEX_LOCK(m)            pthread_mutex_lock((m))
869 #define GSSEAP_MUTEX_UNLOCK(m)          pthread_mutex_unlock((m))
870
871 #define GSSEAP_THREAD_KEY               pthread_key_t
872 #define GSSEAP_KEY_CREATE(k, d)         pthread_key_create((k), (d))
873 #define GSSEAP_GETSPECIFIC(k)           pthread_getspecific((k))
874 #define GSSEAP_SETSPECIFIC(k, d)        pthread_setspecific((k), (d))
875
876 #define GSSEAP_THREAD_ONCE              pthread_once_t
877 #define GSSEAP_ONCE_CALLBACK(cb)        void cb(void)
878 #define GSSEAP_ONCE(o, i)               pthread_once((o), (i))
879 #define GSSEAP_ONCE_INITIALIZER         PTHREAD_ONCE_INIT
880 #define GSSEAP_ONCE_LEAVE               do { } while (0)
881
882 #endif /* WIN32 */
883
884 /* Helper functions */
885 static inline void
886 store_uint16_be(uint16_t val, void *vp)
887 {
888     unsigned char *p = (unsigned char *)vp;
889
890     p[0] = (val >>  8) & 0xff;
891     p[1] = (val      ) & 0xff;
892 }
893
894 static inline uint16_t
895 load_uint16_be(const void *cvp)
896 {
897     const unsigned char *p = (const unsigned char *)cvp;
898
899     return (p[1] | (p[0] << 8));
900 }
901
902 static inline void
903 store_uint32_be(uint32_t val, void *vp)
904 {
905     unsigned char *p = (unsigned char *)vp;
906
907     p[0] = (val >> 24) & 0xff;
908     p[1] = (val >> 16) & 0xff;
909     p[2] = (val >>  8) & 0xff;
910     p[3] = (val      ) & 0xff;
911 }
912
913 static inline uint32_t
914 load_uint32_be(const void *cvp)
915 {
916     const unsigned char *p = (const unsigned char *)cvp;
917
918     return (p[3] | (p[2] << 8)
919             | ((uint32_t) p[1] << 16)
920             | ((uint32_t) p[0] << 24));
921 }
922
923 static inline void
924 store_uint64_be(uint64_t val, void *vp)
925 {
926     unsigned char *p = (unsigned char *)vp;
927
928     p[0] = (unsigned char)((val >> 56) & 0xff);
929     p[1] = (unsigned char)((val >> 48) & 0xff);
930     p[2] = (unsigned char)((val >> 40) & 0xff);
931     p[3] = (unsigned char)((val >> 32) & 0xff);
932     p[4] = (unsigned char)((val >> 24) & 0xff);
933     p[5] = (unsigned char)((val >> 16) & 0xff);
934     p[6] = (unsigned char)((val >>  8) & 0xff);
935     p[7] = (unsigned char)((val      ) & 0xff);
936 }
937
938 static inline uint64_t
939 load_uint64_be(const void *cvp)
940 {
941     const unsigned char *p = (const unsigned char *)cvp;
942
943     return ((uint64_t)load_uint32_be(p) << 32) | load_uint32_be(p + 4);
944 }
945
946 static inline unsigned char *
947 store_buffer(gss_buffer_t buffer, void *vp, int wide_nums)
948 {
949     unsigned char *p = (unsigned char *)vp;
950
951     if (wide_nums) {
952         store_uint64_be(buffer->length, p);
953         p += 8;
954     } else {
955         store_uint32_be(buffer->length, p);
956         p += 4;
957     }
958
959     if (buffer->value != NULL) {
960         memcpy(p, buffer->value, buffer->length);
961         p += buffer->length;
962     }
963
964     return p;
965 }
966
967 static inline unsigned char *
968 load_buffer(const void *cvp, size_t length, gss_buffer_t buffer)
969 {
970     buffer->length = 0;
971     buffer->value = GSSEAP_MALLOC(length);
972     if (buffer->value == NULL)
973         return NULL;
974     buffer->length = length;
975     memcpy(buffer->value, cvp, length);
976     return (unsigned char *)cvp + length;
977 }
978
979 static inline unsigned char *
980 store_oid(gss_OID oid, void *vp)
981 {
982     gss_buffer_desc buf;
983
984     if (oid != GSS_C_NO_OID) {
985         buf.length = oid->length;
986         buf.value = oid->elements;
987     } else {
988         buf.length = 0;
989         buf.value = NULL;
990     }
991
992     return store_buffer(&buf, vp, FALSE);
993 }
994
995 static inline void
996 krbDataToGssBuffer(krb5_data *data, gss_buffer_t buffer)
997 {
998     buffer->value = (void *)data->data;
999     buffer->length = data->length;
1000 }
1001
1002 static inline void
1003 krbPrincComponentToGssBuffer(krb5_principal krbPrinc,
1004                              int index, gss_buffer_t buffer)
1005 {
1006     if (KRB_PRINC_LENGTH(krbPrinc) <= index) {
1007         buffer->value = NULL;
1008         buffer->length = 0;
1009     } else {
1010 #ifdef HAVE_HEIMDAL_VERSION
1011         buffer->value = (void *)KRB_PRINC_NAME(krbPrinc)[index];
1012         buffer->length = strlen((char *)buffer->value);
1013 #else
1014         buffer->value = (void *)krb5_princ_component(NULL, krbPrinc, index)->data;
1015         buffer->length = krb5_princ_component(NULL, krbPrinc, index)->length;
1016 #endif /* HAVE_HEIMDAL_VERSION */
1017     }
1018 }
1019
1020 static inline krb5_error_code
1021 krbPrincUnparseServiceSpecifics(krb5_context krbContext, krb5_principal krbPrinc,
1022                                 gss_buffer_t nameBuf)
1023 {
1024     krb5_error_code result = 0;
1025     if (KRB_PRINC_LENGTH(krbPrinc) > 2) {
1026         /* Acceptor-Service-Specific */
1027         krb5_principal_data ssiPrinc = *krbPrinc;
1028         char *ssi;
1029
1030         KRB_PRINC_LENGTH(&ssiPrinc) -= 2;
1031         KRB_PRINC_NAME(&ssiPrinc) += 2;
1032
1033         result = krb5_unparse_name_flags(krbContext, &ssiPrinc,
1034                                          KRB5_PRINCIPAL_UNPARSE_NO_REALM, &ssi);
1035         if (result != 0)
1036             return result;
1037
1038         nameBuf->value = ssi;
1039         nameBuf->length = strlen(ssi);
1040     } else {
1041         nameBuf->value = NULL;
1042         nameBuf->length = 0;
1043     }
1044
1045     return result;
1046 }
1047
1048 static inline void
1049 krbFreeUnparsedName(krb5_context krbContext, gss_buffer_t nameBuf)
1050 {
1051 #ifdef HAVE_HEIMDAL_VERSION
1052     krb5_xfree((char *) nameBuf->value);
1053 #else
1054     krb5_free_unparsed_name(krbContext, (char *)(nameBuf->value));
1055 #endif
1056     nameBuf->value = NULL;
1057     nameBuf->length = 0;
1058 }
1059
1060 static inline void
1061 krbPrincRealmToGssBuffer(krb5_principal krbPrinc, gss_buffer_t buffer)
1062 {
1063 #ifdef HAVE_HEIMDAL_VERSION
1064     buffer->value = (void *)KRB_PRINC_REALM(krbPrinc);
1065     buffer->length = strlen((char *)buffer->value);
1066 #else
1067     krbDataToGssBuffer(KRB_PRINC_REALM(krbPrinc), buffer);
1068 #endif
1069 }
1070
1071 static inline void
1072 gssBufferToKrbData(gss_buffer_t buffer, krb5_data *data)
1073 {
1074     data->data = (char *)buffer->value;
1075     data->length = buffer->length;
1076 }
1077
1078 /* util_tld.c */
1079 struct gss_eap_status_info;
1080
1081 struct gss_eap_thread_local_data {
1082     krb5_context krbContext;
1083     struct gss_eap_status_info *statusInfo;
1084 };
1085
1086 struct gss_eap_thread_local_data *
1087 gssEapGetThreadLocalData(void);
1088
1089 void
1090 gssEapDestroyStatusInfo(struct gss_eap_status_info *status);
1091
1092 void
1093 gssEapDestroyKrbContext(krb5_context context);
1094
1095 #ifdef __cplusplus
1096 }
1097 #endif
1098
1099 #ifdef GSSEAP_ENABLE_ACCEPTOR
1100 #include "util_json.h"
1101 #include "util_attr.h"
1102 #include "util_base64.h"
1103 #endif /* GSSEAP_ENABLE_ACCEPTOR */
1104 #ifdef GSSEAP_ENABLE_REAUTH
1105 #include "util_reauth.h"
1106 #endif
1107
1108 #endif /* _UTIL_H_ */