Ignore empty realms comparing acceptor name hint
[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 gssEapSetCredService(OM_uint32 *minor,
276                      gss_cred_id_t cred,
277                      const gss_name_t target);
278
279 OM_uint32
280 gssEapResolveInitiatorCred(OM_uint32 *minor,
281                            const gss_cred_id_t cred,
282                            const gss_name_t target,
283                            gss_cred_id_t *resolvedCred);
284
285 int gssEapCredAvailable(gss_cred_id_t cred, gss_OID mech);
286
287 OM_uint32
288 gssEapInquireCred(OM_uint32 *minor,
289                   gss_cred_id_t cred,
290                   gss_name_t *name,
291                   OM_uint32 *pLifetime,
292                   gss_cred_usage_t *cred_usage,
293                   gss_OID_set *mechanisms);
294
295 /* util_crypt.c */
296 int
297 gssEapEncrypt(krb5_context context, int dce_style, size_t ec,
298               size_t rrc,
299 #ifdef HAVE_HEIMDAL_VERSION
300               krb5_crypto crypto,
301 #else
302               krb5_keyblock *key,
303 #endif
304               int usage,
305               gss_iov_buffer_desc *iov, int iov_count);
306
307 int
308 gssEapDecrypt(krb5_context context, int dce_style, size_t ec,
309               size_t rrc,
310 #ifdef HAVE_HEIMDAL_VERSION
311               krb5_crypto crypto,
312 #else
313               krb5_keyblock *key,
314 #endif
315               int usage,
316               gss_iov_buffer_desc *iov, int iov_count);
317
318 int
319 gssEapMapCryptoFlag(OM_uint32 type);
320
321 gss_iov_buffer_t
322 gssEapLocateIov(gss_iov_buffer_desc *iov,
323                 int iov_count,
324                 OM_uint32 type);
325
326 void
327 gssEapIovMessageLength(gss_iov_buffer_desc *iov,
328                        int iov_count,
329                        size_t *data_length,
330                        size_t *assoc_data_length);
331
332 void
333 gssEapReleaseIov(gss_iov_buffer_desc *iov, int iov_count);
334
335 int
336 gssEapIsIntegrityOnly(gss_iov_buffer_desc *iov, int iov_count);
337
338 int
339 gssEapAllocIov(gss_iov_buffer_t iov, size_t size);
340
341 OM_uint32
342 gssEapDeriveRfc3961Key(OM_uint32 *minor,
343                        const unsigned char *key,
344                        size_t keyLength,
345                        krb5_enctype enctype,
346                        krb5_keyblock *pKey);
347
348 /* util_krb.c */
349
350 #ifndef KRB_MALLOC
351 /*
352  * If your Kerberos library uses a different allocator to your
353  * GSS mechanism glue, then you might wish to define these in
354  * config.h or elsewhere. This should eventually go away when
355  * we no longer need to allocate memory that is freed by the
356  * Kerberos library.
357  */
358 #define KRB_CALLOC                      calloc
359 #define KRB_MALLOC                      malloc
360 #define KRB_FREE                        free
361 #define KRB_REALLOC                     realloc
362 #endif /* KRB_MALLOC */
363
364 #ifdef HAVE_HEIMDAL_VERSION
365
366 #define KRB_TIME_FOREVER        ((time_t)~0L)
367
368 #define KRB_KEY_TYPE(key)       ((key)->keytype)
369 #define KRB_KEY_DATA(key)       ((key)->keyvalue.data)
370 #define KRB_KEY_LENGTH(key)     ((key)->keyvalue.length)
371
372 #define KRB_PRINC_LENGTH(princ) ((princ)->name.name_string.len)
373 #define KRB_PRINC_TYPE(princ)   ((princ)->name.name_type)
374 #define KRB_PRINC_NAME(princ)   ((princ)->name.name_string.val)
375 #define KRB_PRINC_REALM(princ)  ((princ)->realm)
376
377 #define KRB_KT_ENT_KEYBLOCK(e)  (&(e)->keyblock)
378 #define KRB_KT_ENT_FREE(c, e)   krb5_kt_free_entry((c), (e))
379
380 #define KRB_CRYPTO_CONTEXT(ctx) (krbCrypto)
381
382 #define KRB_DATA_INIT(d)        krb5_data_zero((d))
383
384 #define KRB_CHECKSUM_TYPE(c)    ((c)->cksumtype)
385 #define KRB_CHECKSUM_LENGTH(c)  ((c)->checksum.length)
386 #define KRB_CHECKSUM_DATA(c)    ((c)->checksum.data)
387
388 #define KRB_CHECKSUM_INIT(cksum, type, d)      do { \
389         (cksum)->cksumtype = (type);                \
390         (cksum)->checksum.length = (d)->length;     \
391         (cksum)->checksum.data = (d)->value;        \
392     } while (0)
393
394 #else
395
396 #define KRB_TIME_FOREVER        KRB5_INT32_MAX
397
398 #define KRB_KEY_TYPE(key)       ((key)->enctype)
399 #define KRB_KEY_DATA(key)       ((key)->contents)
400 #define KRB_KEY_LENGTH(key)     ((key)->length)
401
402 #define KRB_PRINC_LENGTH(princ) (krb5_princ_size(NULL, (princ)))
403 #define KRB_PRINC_TYPE(princ)   (krb5_princ_type(NULL, (princ)))
404 #define KRB_PRINC_NAME(princ)   (krb5_princ_name(NULL, (princ)))
405 #define KRB_PRINC_REALM(princ)  (krb5_princ_realm(NULL, (princ)))
406
407 #define KRB_KT_ENT_KEYBLOCK(e)  (&(e)->key)
408 #define KRB_KT_ENT_FREE(c, e)   krb5_free_keytab_entry_contents((c), (e))
409
410 #define KRB_CRYPTO_CONTEXT(ctx) (&(ctx)->rfc3961Key)
411
412 #define KRB_DATA_INIT(d)        do {        \
413         (d)->magic = KV5M_DATA;             \
414         (d)->length = 0;                    \
415         (d)->data = NULL;                   \
416     } while (0)
417
418 #define KRB_CHECKSUM_TYPE(c)    ((c)->checksum_type)
419 #define KRB_CHECKSUM_LENGTH(c)  ((c)->length)
420 #define KRB_CHECKSUM_DATA(c)    ((c)->contents)
421
422 #define KRB_CHECKSUM_INIT(cksum, type, d)      do { \
423         (cksum)->checksum_type = (type);            \
424         (cksum)->length = (d)->length;              \
425         (cksum)->contents = (d)->value;             \
426     } while (0)
427
428 #endif /* HAVE_HEIMDAL_VERSION */
429
430 #define KRB_KEY_INIT(key)       do {        \
431         KRB_KEY_TYPE(key) = ENCTYPE_NULL;   \
432         KRB_KEY_DATA(key) = NULL;           \
433         KRB_KEY_LENGTH(key) = 0;            \
434     } while (0)
435
436 #define GSSEAP_KRB_INIT(ctx) do {                   \
437         OM_uint32 tmpMajor;                         \
438                                                     \
439         tmpMajor  = gssEapKerberosInit(minor, ctx); \
440         if (GSS_ERROR(tmpMajor)) {                  \
441             return tmpMajor;                        \
442         }                                           \
443     } while (0)
444
445 OM_uint32
446 gssEapKerberosInit(OM_uint32 *minor, krb5_context *context);
447
448 OM_uint32
449 rfc3961ChecksumTypeForKey(OM_uint32 *minor,
450                           krb5_keyblock *key,
451                           krb5_cksumtype *cksumtype);
452
453 krb5_error_code
454 krbCryptoLength(krb5_context krbContext,
455 #ifdef HAVE_HEIMDAL_VERSION
456                 krb5_crypto krbCrypto,
457 #else
458                 krb5_keyblock *key,
459 #endif
460                 int type,
461                 size_t *length);
462
463 krb5_error_code
464 krbPaddingLength(krb5_context krbContext,
465 #ifdef HAVE_HEIMDAL_VERSION
466                  krb5_crypto krbCrypto,
467 #else
468                  krb5_keyblock *key,
469 #endif
470                  size_t dataLength,
471                  size_t *padLength);
472
473 krb5_error_code
474 krbBlockSize(krb5_context krbContext,
475 #ifdef HAVE_HEIMDAL_VERSION
476                  krb5_crypto krbCrypto,
477 #else
478                  krb5_keyblock *key,
479 #endif
480                  size_t *blockSize);
481
482 krb5_error_code
483 krbEnctypeToString(krb5_context krbContext,
484                    krb5_enctype enctype,
485                    const char *prefix,
486                    gss_buffer_t string);
487
488 krb5_error_code
489 krbMakeAuthDataKdcIssued(krb5_context context,
490                          const krb5_keyblock *key,
491                          krb5_const_principal issuer,
492 #ifdef HAVE_HEIMDAL_VERSION
493                          const AuthorizationData *authdata,
494                          AuthorizationData *adKdcIssued
495 #else
496                          krb5_authdata *const *authdata,
497                          krb5_authdata ***adKdcIssued
498 #endif
499                          );
500
501 krb5_error_code
502 krbMakeCred(krb5_context context,
503             krb5_auth_context authcontext,
504             krb5_creds *creds,
505             krb5_data *data);
506
507 /* util_lucid.c */
508 OM_uint32
509 gssEapExportLucidSecContext(OM_uint32 *minor,
510                             gss_ctx_id_t ctx,
511                             const gss_OID desiredObject,
512                             gss_buffer_set_t *data_set);
513
514 /* util_mech.c */
515 extern gss_OID GSS_EAP_MECHANISM;
516
517 #define OID_FLAG_NULL_VALID                 0x00000001
518 #define OID_FLAG_FAMILY_MECH_VALID          0x00000002
519 #define OID_FLAG_MAP_NULL_TO_DEFAULT_MECH   0x00000004
520 #define OID_FLAG_MAP_FAMILY_MECH_TO_NULL    0x00000008
521
522 OM_uint32
523 gssEapCanonicalizeOid(OM_uint32 *minor,
524                       const gss_OID oid,
525                       OM_uint32 flags,
526                       gss_OID *pOid);
527
528 OM_uint32
529 gssEapReleaseOid(OM_uint32 *minor, gss_OID *oid);
530
531 OM_uint32
532 gssEapDefaultMech(OM_uint32 *minor,
533                   gss_OID *oid);
534
535 OM_uint32
536 gssEapIndicateMechs(OM_uint32 *minor,
537                     gss_OID_set *mechs);
538
539 OM_uint32
540 gssEapEnctypeToOid(OM_uint32 *minor,
541                    krb5_enctype enctype,
542                    gss_OID *pOid);
543
544 OM_uint32
545 gssEapOidToEnctype(OM_uint32 *minor,
546                    const gss_OID oid,
547                    krb5_enctype *enctype);
548
549 int
550 gssEapIsMechanismOid(const gss_OID oid);
551
552 int
553 gssEapIsConcreteMechanismOid(const gss_OID oid);
554
555 OM_uint32
556 gssEapValidateMechs(OM_uint32 *minor,
557                    const gss_OID_set mechs);
558
559 gss_buffer_t
560 gssEapOidToSaslName(const gss_OID oid);
561
562 gss_OID
563 gssEapSaslNameToOid(const gss_buffer_t name);
564
565 /* util_moonshot.c */
566 OM_uint32
567 libMoonshotResolveDefaultIdentity(OM_uint32 *minor,
568                                   const gss_cred_id_t cred,
569                                   gss_name_t *pName);
570
571 OM_uint32
572 libMoonshotResolveInitiatorCred(OM_uint32 *minor,
573                                 gss_cred_id_t cred,
574                                 const gss_name_t targetName);
575
576 /* util_name.c */
577 #define EXPORT_NAME_FLAG_OID                    0x1
578 #define EXPORT_NAME_FLAG_COMPOSITE              0x2
579 #define EXPORT_NAME_FLAG_ALLOW_COMPOSITE        0x4
580
581 OM_uint32 gssEapAllocName(OM_uint32 *minor, gss_name_t *pName);
582 OM_uint32 gssEapReleaseName(OM_uint32 *minor, gss_name_t *pName);
583 OM_uint32 gssEapExportName(OM_uint32 *minor,
584                            const gss_name_t name,
585                            gss_buffer_t exportedName);
586 OM_uint32 gssEapExportNameInternal(OM_uint32 *minor,
587                                    const gss_name_t name,
588                                    gss_buffer_t exportedName,
589                                    OM_uint32 flags);
590 OM_uint32 gssEapImportName(OM_uint32 *minor,
591                            const gss_buffer_t input_name_buffer,
592                            const gss_OID input_name_type,
593                            const gss_OID input_mech_type,
594                            gss_name_t *output_name);
595 OM_uint32 gssEapImportNameInternal(OM_uint32 *minor,
596                                    const gss_buffer_t input_name_buffer,
597                                    gss_name_t *output_name,
598                                    OM_uint32 flags);
599 OM_uint32
600 gssEapDuplicateName(OM_uint32 *minor,
601                     const gss_name_t input_name,
602                     gss_name_t *dest_name);
603
604 OM_uint32
605 gssEapCanonicalizeName(OM_uint32 *minor,
606                        const gss_name_t input_name,
607                        const gss_OID mech_type,
608                        gss_name_t *dest_name);
609
610 OM_uint32
611 gssEapDisplayName(OM_uint32 *minor,
612                   gss_name_t name,
613                   gss_buffer_t output_name_buffer,
614                   gss_OID *output_name_type);
615
616 #define COMPARE_NAME_FLAG_IGNORE_EMPTY_REALMS   0x1
617
618 OM_uint32
619 gssEapCompareName(OM_uint32 *minor,
620                   gss_name_t name1,
621                   gss_name_t name2,
622                   OM_uint32 flags,
623                   int *name_equal);
624
625 /* util_oid.c */
626 OM_uint32
627 composeOid(OM_uint32 *minor_status,
628            const char *prefix,
629            size_t prefix_len,
630            int suffix,
631            gss_OID_desc *oid);
632
633 OM_uint32
634 decomposeOid(OM_uint32 *minor_status,
635              const char *prefix,
636              size_t prefix_len,
637              gss_OID_desc *oid,
638              int *suffix) ;
639
640 OM_uint32
641 duplicateOid(OM_uint32 *minor_status,
642              const gss_OID_desc * const oid,
643              gss_OID *new_oid);
644
645 OM_uint32
646 duplicateOidSet(OM_uint32 *minor,
647                 const gss_OID_set src,
648                 gss_OID_set *dst);
649
650 static inline int
651 oidEqual(const gss_OID_desc *o1, const gss_OID_desc *o2)
652 {
653     if (o1 == GSS_C_NO_OID)
654         return (o2 == GSS_C_NO_OID);
655     else if (o2 == GSS_C_NO_OID)
656         return (o1 == GSS_C_NO_OID);
657     else
658         return (o1->length == o2->length &&
659                 memcmp(o1->elements, o2->elements, o1->length) == 0);
660 }
661
662 /* util_ordering.c */
663 OM_uint32
664 sequenceInternalize(OM_uint32 *minor,
665                     void **vqueue,
666                     unsigned char **buf,
667                     size_t *lenremain);
668
669 OM_uint32
670 sequenceExternalize(OM_uint32 *minor,
671                     void *vqueue,
672                     unsigned char **buf,
673                     size_t *lenremain);
674
675 size_t
676 sequenceSize(void *vqueue);
677
678 OM_uint32
679 sequenceFree(OM_uint32 *minor, void **vqueue);
680
681 OM_uint32
682 sequenceCheck(OM_uint32 *minor, void **vqueue, uint64_t seqnum);
683
684 OM_uint32
685 sequenceInit(OM_uint32 *minor, void **vqueue, uint64_t seqnum,
686              int do_replay, int do_sequence, int wide_nums);
687
688 /* util_sm.c */
689 enum gss_eap_state {
690     GSSEAP_STATE_INITIAL        = 0x01,     /* initial state */
691     GSSEAP_STATE_AUTHENTICATE   = 0x02,     /* exchange EAP messages */
692     GSSEAP_STATE_INITIATOR_EXTS = 0x04,     /* initiator extensions */
693     GSSEAP_STATE_ACCEPTOR_EXTS  = 0x08,     /* acceptor extensions */
694 #ifdef GSSEAP_ENABLE_REAUTH
695     GSSEAP_STATE_REAUTHENTICATE = 0x10,     /* GSS reauthentication messages */
696 #endif
697     GSSEAP_STATE_ESTABLISHED    = 0x20,     /* context established */
698     GSSEAP_STATE_ALL            = 0x3F
699 };
700
701 #define GSSEAP_STATE_NEXT(s)    ((s) << 1)
702
703 #define GSSEAP_SM_STATE(ctx)                ((ctx)->state)
704
705 #ifdef GSSEAP_DEBUG
706 void gssEapSmTransition(gss_ctx_id_t ctx, enum gss_eap_state state);
707 #define GSSEAP_SM_TRANSITION(ctx, state)    gssEapSmTransition((ctx), (state))
708 #else
709 #define GSSEAP_SM_TRANSITION(ctx, newstate)    do { (ctx)->state = (newstate); } while (0)
710 #endif
711
712 #define GSSEAP_SM_TRANSITION_NEXT(ctx)      GSSEAP_SM_TRANSITION((ctx), GSSEAP_STATE_NEXT(GSSEAP_SM_STATE((ctx))))
713
714 /* state machine entry */
715 struct gss_eap_sm {
716     OM_uint32 inputTokenType;
717     OM_uint32 outputTokenType;
718     enum gss_eap_state validStates;
719     OM_uint32 itokFlags;
720     OM_uint32 (*processToken)(OM_uint32 *,
721                               gss_cred_id_t,
722                               gss_ctx_id_t,
723                               gss_name_t,
724                               gss_OID,
725                               OM_uint32,
726                               OM_uint32,
727                               gss_channel_bindings_t,
728                               gss_buffer_t,
729                               gss_buffer_t,
730                               OM_uint32 *);
731 };
732
733 /* state machine flags, set by handler */
734 #define SM_FLAG_FORCE_SEND_TOKEN            0x00000001  /* send token even if no inner tokens */
735 #define SM_FLAG_OUTPUT_TOKEN_CRITICAL       0x00000002  /* output token is critical */
736
737 /* state machine flags, set by state machine */
738 #define SM_FLAG_INPUT_TOKEN_CRITICAL        0x10000000  /* input token was critical */
739
740 #define SM_ITOK_FLAG_REQUIRED               0x00000001  /* received tokens must be present */
741
742 OM_uint32
743 gssEapSmStep(OM_uint32 *minor,
744              gss_cred_id_t cred,
745              gss_ctx_id_t ctx,
746              gss_name_t target,
747              gss_OID mech,
748              OM_uint32 reqFlags,
749              OM_uint32 timeReq,
750              gss_channel_bindings_t chanBindings,
751              gss_buffer_t inputToken,
752              gss_buffer_t outputToken,
753              struct gss_eap_sm *sm,
754              size_t smCount);
755
756 void
757 gssEapSmTransition(gss_ctx_id_t ctx, enum gss_eap_state state);
758
759 /* util_token.c */
760 struct gss_eap_token_buffer_set {
761     gss_buffer_set_desc buffers; /* pointers only */
762     OM_uint32 *types;
763 };
764
765 OM_uint32
766 gssEapEncodeInnerTokens(OM_uint32 *minor,
767                         struct gss_eap_token_buffer_set *tokens,
768                         gss_buffer_t buffer);
769 OM_uint32
770 gssEapDecodeInnerTokens(OM_uint32 *minor,
771                         const gss_buffer_t buffer,
772                         struct gss_eap_token_buffer_set *tokens);
773
774 OM_uint32
775 gssEapReleaseInnerTokens(OM_uint32 *minor,
776                          struct gss_eap_token_buffer_set *tokens,
777                          int freeBuffers);
778
779 OM_uint32
780 gssEapAllocInnerTokens(OM_uint32 *minor,
781                        size_t count,
782                        struct gss_eap_token_buffer_set *tokens);
783
784 size_t
785 tokenSize(const gss_OID_desc *mech, size_t body_size);
786
787 void
788 makeTokenHeader(const gss_OID_desc *mech,
789                 size_t body_size,
790                 unsigned char **buf,
791                 enum gss_eap_token_type tok_type);
792
793 OM_uint32
794 verifyTokenHeader(OM_uint32 *minor,
795                   gss_OID mech,
796                   size_t *body_size,
797                   unsigned char **buf_in,
798                   size_t toksize_in,
799                   enum gss_eap_token_type *ret_tok_type);
800
801 /* Helper macros */
802
803 #ifndef GSSEAP_MALLOC
804 #define GSSEAP_CALLOC                   calloc
805 #define GSSEAP_MALLOC                   malloc
806 #define GSSEAP_FREE                     free
807 #define GSSEAP_REALLOC                  realloc
808 #endif
809
810 #ifndef GSSAPI_CALLCONV
811 #define GSSAPI_CALLCONV                 KRB5_CALLCONV
812 #endif
813
814 #ifndef GSSEAP_ASSERT
815 #include <assert.h>
816 #define GSSEAP_ASSERT(x)                assert((x))
817 #endif /* !GSSEAP_ASSERT */
818
819 #ifdef WIN32
820 #define GSSEAP_CONSTRUCTOR
821 #define GSSEAP_DESTRUCTOR
822 #else
823 #define GSSEAP_CONSTRUCTOR              __attribute__((constructor))
824 #define GSSEAP_DESTRUCTOR               __attribute__((destructor))
825 #endif
826
827 #define GSSEAP_NOT_IMPLEMENTED          do {            \
828         GSSEAP_ASSERT(0 && "not implemented");          \
829         *minor = ENOSYS;                                \
830         return GSS_S_FAILURE;                           \
831     } while (0)
832
833 #ifdef WIN32
834
835 #include <winbase.h>
836
837 #define GSSEAP_GET_LAST_ERROR()         (GetLastError()) /* XXX FIXME */
838
839 #define GSSEAP_MUTEX                    CRITICAL_SECTION
840 #define GSSEAP_MUTEX_INIT(m)            (InitializeCriticalSection((m)), 0)
841 #define GSSEAP_MUTEX_DESTROY(m)         DeleteCriticalSection((m))
842 #define GSSEAP_MUTEX_LOCK(m)            EnterCriticalSection((m))
843 #define GSSEAP_MUTEX_UNLOCK(m)          LeaveCriticalSection((m))
844 #define GSSEAP_ONCE_LEAVE               do { return TRUE; } while (0)
845
846 /* Thread-local is handled separately */
847
848 #define GSSEAP_THREAD_ONCE              INIT_ONCE
849 #define GSSEAP_ONCE_CALLBACK(cb)        BOOL CALLBACK cb(PINIT_ONCE InitOnce, PVOID Parameter, PVOID *Context)
850 #define GSSEAP_ONCE(o, i)               InitOnceExecuteOnce((o), (i), NULL, NULL)
851 #define GSSEAP_ONCE_INITIALIZER         INIT_ONCE_STATIC_INIT
852
853 #else
854
855 #include <pthread.h>
856
857 #define GSSEAP_GET_LAST_ERROR()         (errno)
858
859 #define GSSEAP_MUTEX                    pthread_mutex_t
860 #define GSSEAP_MUTEX_INIT(m)            pthread_mutex_init((m), NULL)
861 #define GSSEAP_MUTEX_DESTROY(m)         pthread_mutex_destroy((m))
862 #define GSSEAP_MUTEX_LOCK(m)            pthread_mutex_lock((m))
863 #define GSSEAP_MUTEX_UNLOCK(m)          pthread_mutex_unlock((m))
864
865 #define GSSEAP_THREAD_KEY               pthread_key_t
866 #define GSSEAP_KEY_CREATE(k, d)         pthread_key_create((k), (d))
867 #define GSSEAP_GETSPECIFIC(k)           pthread_getspecific((k))
868 #define GSSEAP_SETSPECIFIC(k, d)        pthread_setspecific((k), (d))
869
870 #define GSSEAP_THREAD_ONCE              pthread_once_t
871 #define GSSEAP_ONCE_CALLBACK(cb)        void cb(void)
872 #define GSSEAP_ONCE(o, i)               pthread_once((o), (i))
873 #define GSSEAP_ONCE_INITIALIZER         PTHREAD_ONCE_INIT
874 #define GSSEAP_ONCE_LEAVE               do { } while (0)
875
876 #endif /* WIN32 */
877
878 /* Helper functions */
879 static inline void
880 store_uint16_be(uint16_t val, void *vp)
881 {
882     unsigned char *p = (unsigned char *)vp;
883
884     p[0] = (val >>  8) & 0xff;
885     p[1] = (val      ) & 0xff;
886 }
887
888 static inline uint16_t
889 load_uint16_be(const void *cvp)
890 {
891     const unsigned char *p = (const unsigned char *)cvp;
892
893     return (p[1] | (p[0] << 8));
894 }
895
896 static inline void
897 store_uint32_be(uint32_t val, void *vp)
898 {
899     unsigned char *p = (unsigned char *)vp;
900
901     p[0] = (val >> 24) & 0xff;
902     p[1] = (val >> 16) & 0xff;
903     p[2] = (val >>  8) & 0xff;
904     p[3] = (val      ) & 0xff;
905 }
906
907 static inline uint32_t
908 load_uint32_be(const void *cvp)
909 {
910     const unsigned char *p = (const unsigned char *)cvp;
911
912     return (p[3] | (p[2] << 8)
913             | ((uint32_t) p[1] << 16)
914             | ((uint32_t) p[0] << 24));
915 }
916
917 static inline void
918 store_uint64_be(uint64_t val, void *vp)
919 {
920     unsigned char *p = (unsigned char *)vp;
921
922     p[0] = (unsigned char)((val >> 56) & 0xff);
923     p[1] = (unsigned char)((val >> 48) & 0xff);
924     p[2] = (unsigned char)((val >> 40) & 0xff);
925     p[3] = (unsigned char)((val >> 32) & 0xff);
926     p[4] = (unsigned char)((val >> 24) & 0xff);
927     p[5] = (unsigned char)((val >> 16) & 0xff);
928     p[6] = (unsigned char)((val >>  8) & 0xff);
929     p[7] = (unsigned char)((val      ) & 0xff);
930 }
931
932 static inline uint64_t
933 load_uint64_be(const void *cvp)
934 {
935     const unsigned char *p = (const unsigned char *)cvp;
936
937     return ((uint64_t)load_uint32_be(p) << 32) | load_uint32_be(p + 4);
938 }
939
940 static inline unsigned char *
941 store_buffer(gss_buffer_t buffer, void *vp, int wide_nums)
942 {
943     unsigned char *p = (unsigned char *)vp;
944
945     if (wide_nums) {
946         store_uint64_be(buffer->length, p);
947         p += 8;
948     } else {
949         store_uint32_be(buffer->length, p);
950         p += 4;
951     }
952
953     if (buffer->value != NULL) {
954         memcpy(p, buffer->value, buffer->length);
955         p += buffer->length;
956     }
957
958     return p;
959 }
960
961 static inline unsigned char *
962 load_buffer(const void *cvp, size_t length, gss_buffer_t buffer)
963 {
964     buffer->length = 0;
965     buffer->value = GSSEAP_MALLOC(length);
966     if (buffer->value == NULL)
967         return NULL;
968     buffer->length = length;
969     memcpy(buffer->value, cvp, length);
970     return (unsigned char *)cvp + length;
971 }
972
973 static inline unsigned char *
974 store_oid(gss_OID oid, void *vp)
975 {
976     gss_buffer_desc buf;
977
978     if (oid != GSS_C_NO_OID) {
979         buf.length = oid->length;
980         buf.value = oid->elements;
981     } else {
982         buf.length = 0;
983         buf.value = NULL;
984     }
985
986     return store_buffer(&buf, vp, FALSE);
987 }
988
989 static inline void
990 krbDataToGssBuffer(krb5_data *data, gss_buffer_t buffer)
991 {
992     buffer->value = (void *)data->data;
993     buffer->length = data->length;
994 }
995
996 static inline void
997 krbPrincComponentToGssBuffer(krb5_principal krbPrinc,
998                              int index, gss_buffer_t buffer)
999 {
1000 #ifdef HAVE_HEIMDAL_VERSION
1001     buffer->value = (void *)KRB_PRINC_NAME(krbPrinc)[index];
1002     buffer->length = strlen((char *)buffer->value);
1003 #else
1004     buffer->value = (void *)krb5_princ_component(NULL, krbPrinc, index)->data;
1005     buffer->length = krb5_princ_component(NULL, krbPrinc, index)->length;
1006 #endif /* HAVE_HEIMDAL_VERSION */
1007 }
1008
1009 static inline void
1010 krbPrincRealmToGssBuffer(krb5_principal krbPrinc, gss_buffer_t buffer)
1011 {
1012 #ifdef HAVE_HEIMDAL_VERSION
1013     buffer->value = (void *)KRB_PRINC_REALM(krbPrinc);
1014     buffer->length = strlen((char *)buffer->value);
1015 #else
1016     krbDataToGssBuffer(KRB_PRINC_REALM(krbPrinc), buffer);
1017 #endif
1018 }
1019
1020 static inline void
1021 gssBufferToKrbData(gss_buffer_t buffer, krb5_data *data)
1022 {
1023     data->data = (char *)buffer->value;
1024     data->length = buffer->length;
1025 }
1026
1027 /* util_tld.c */
1028 struct gss_eap_status_info;
1029
1030 struct gss_eap_thread_local_data {
1031     krb5_context krbContext;
1032     struct gss_eap_status_info *statusInfo;
1033 };
1034
1035 struct gss_eap_thread_local_data *
1036 gssEapGetThreadLocalData(void);
1037
1038 void
1039 gssEapDestroyStatusInfo(struct gss_eap_status_info *status);
1040
1041 void
1042 gssEapDestroyKrbContext(krb5_context context);
1043
1044 #ifdef __cplusplus
1045 }
1046 #endif
1047
1048 #ifdef GSSEAP_ENABLE_ACCEPTOR
1049 #include "util_json.h"
1050 #include "util_attr.h"
1051 #include "util_base64.h"
1052 #endif /* GSSEAP_ENABLE_ACCEPTOR */
1053 #ifdef GSSEAP_ENABLE_REAUTH
1054 #include "util_reauth.h"
1055 #endif
1056
1057 #endif /* _UTIL_H_ */