EAP Channel binding support
[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 #define KRB_PRINC_COMPONENT(princ, component) \
407         (krb5_princ_component(NULL, (princ), (component)))
408
409 #define KRB_KT_ENT_KEYBLOCK(e)  (&(e)->key)
410 #define KRB_KT_ENT_FREE(c, e)   krb5_free_keytab_entry_contents((c), (e))
411
412 #define KRB_CRYPTO_CONTEXT(ctx) (&(ctx)->rfc3961Key)
413
414 #define KRB_DATA_INIT(d)        do {        \
415         (d)->magic = KV5M_DATA;             \
416         (d)->length = 0;                    \
417         (d)->data = NULL;                   \
418     } while (0)
419
420 #define KRB_CHECKSUM_TYPE(c)    ((c)->checksum_type)
421 #define KRB_CHECKSUM_LENGTH(c)  ((c)->length)
422 #define KRB_CHECKSUM_DATA(c)    ((c)->contents)
423
424 #define KRB_CHECKSUM_INIT(cksum, type, d)      do { \
425         (cksum)->checksum_type = (type);            \
426         (cksum)->length = (d)->length;              \
427         (cksum)->contents = (d)->value;             \
428     } while (0)
429
430 #endif /* HAVE_HEIMDAL_VERSION */
431
432 #define KRB_KEY_INIT(key)       do {        \
433         KRB_KEY_TYPE(key) = ENCTYPE_NULL;   \
434         KRB_KEY_DATA(key) = NULL;           \
435         KRB_KEY_LENGTH(key) = 0;            \
436     } while (0)
437
438 #define GSSEAP_KRB_INIT(ctx) do {                   \
439         OM_uint32 tmpMajor;                         \
440                                                     \
441         tmpMajor  = gssEapKerberosInit(minor, ctx); \
442         if (GSS_ERROR(tmpMajor)) {                  \
443             return tmpMajor;                        \
444         }                                           \
445     } while (0)
446
447 OM_uint32
448 gssEapKerberosInit(OM_uint32 *minor, krb5_context *context);
449
450 OM_uint32
451 rfc3961ChecksumTypeForKey(OM_uint32 *minor,
452                           krb5_keyblock *key,
453                           krb5_cksumtype *cksumtype);
454
455 krb5_error_code
456 krbCryptoLength(krb5_context krbContext,
457 #ifdef HAVE_HEIMDAL_VERSION
458                 krb5_crypto krbCrypto,
459 #else
460                 krb5_keyblock *key,
461 #endif
462                 int type,
463                 size_t *length);
464
465 krb5_error_code
466 krbPaddingLength(krb5_context krbContext,
467 #ifdef HAVE_HEIMDAL_VERSION
468                  krb5_crypto krbCrypto,
469 #else
470                  krb5_keyblock *key,
471 #endif
472                  size_t dataLength,
473                  size_t *padLength);
474
475 krb5_error_code
476 krbBlockSize(krb5_context krbContext,
477 #ifdef HAVE_HEIMDAL_VERSION
478                  krb5_crypto krbCrypto,
479 #else
480                  krb5_keyblock *key,
481 #endif
482                  size_t *blockSize);
483
484 krb5_error_code
485 krbEnctypeToString(krb5_context krbContext,
486                    krb5_enctype enctype,
487                    const char *prefix,
488                    gss_buffer_t string);
489
490 krb5_error_code
491 krbMakeAuthDataKdcIssued(krb5_context context,
492                          const krb5_keyblock *key,
493                          krb5_const_principal issuer,
494 #ifdef HAVE_HEIMDAL_VERSION
495                          const AuthorizationData *authdata,
496                          AuthorizationData *adKdcIssued
497 #else
498                          krb5_authdata *const *authdata,
499                          krb5_authdata ***adKdcIssued
500 #endif
501                          );
502
503 krb5_error_code
504 krbMakeCred(krb5_context context,
505             krb5_auth_context authcontext,
506             krb5_creds *creds,
507             krb5_data *data);
508
509 /* util_lucid.c */
510 OM_uint32
511 gssEapExportLucidSecContext(OM_uint32 *minor,
512                             gss_ctx_id_t ctx,
513                             const gss_OID desiredObject,
514                             gss_buffer_set_t *data_set);
515
516 /* util_mech.c */
517 extern gss_OID GSS_EAP_MECHANISM;
518
519 #define OID_FLAG_NULL_VALID                 0x00000001
520 #define OID_FLAG_FAMILY_MECH_VALID          0x00000002
521 #define OID_FLAG_MAP_NULL_TO_DEFAULT_MECH   0x00000004
522 #define OID_FLAG_MAP_FAMILY_MECH_TO_NULL    0x00000008
523
524 OM_uint32
525 gssEapCanonicalizeOid(OM_uint32 *minor,
526                       const gss_OID oid,
527                       OM_uint32 flags,
528                       gss_OID *pOid);
529
530 OM_uint32
531 gssEapReleaseOid(OM_uint32 *minor, gss_OID *oid);
532
533 OM_uint32
534 gssEapDefaultMech(OM_uint32 *minor,
535                   gss_OID *oid);
536
537 OM_uint32
538 gssEapIndicateMechs(OM_uint32 *minor,
539                     gss_OID_set *mechs);
540
541 OM_uint32
542 gssEapEnctypeToOid(OM_uint32 *minor,
543                    krb5_enctype enctype,
544                    gss_OID *pOid);
545
546 OM_uint32
547 gssEapOidToEnctype(OM_uint32 *minor,
548                    const gss_OID oid,
549                    krb5_enctype *enctype);
550
551 int
552 gssEapIsMechanismOid(const gss_OID oid);
553
554 int
555 gssEapIsConcreteMechanismOid(const gss_OID oid);
556
557 OM_uint32
558 gssEapValidateMechs(OM_uint32 *minor,
559                    const gss_OID_set mechs);
560
561 gss_buffer_t
562 gssEapOidToSaslName(const gss_OID oid);
563
564 gss_OID
565 gssEapSaslNameToOid(const gss_buffer_t name);
566
567 /* util_moonshot.c */
568 OM_uint32
569 libMoonshotResolveDefaultIdentity(OM_uint32 *minor,
570                                   const gss_cred_id_t cred,
571                                   gss_name_t *pName);
572
573 OM_uint32
574 libMoonshotResolveInitiatorCred(OM_uint32 *minor,
575                                 gss_cred_id_t cred,
576                                 const gss_name_t targetName);
577
578 /* util_name.c */
579 #define EXPORT_NAME_FLAG_OID                    0x1
580 #define EXPORT_NAME_FLAG_COMPOSITE              0x2
581 #define EXPORT_NAME_FLAG_ALLOW_COMPOSITE        0x4
582
583 OM_uint32 gssEapAllocName(OM_uint32 *minor, gss_name_t *pName);
584 OM_uint32 gssEapReleaseName(OM_uint32 *minor, gss_name_t *pName);
585 OM_uint32 gssEapExportName(OM_uint32 *minor,
586                            const gss_name_t name,
587                            gss_buffer_t exportedName);
588 OM_uint32 gssEapExportNameInternal(OM_uint32 *minor,
589                                    const gss_name_t name,
590                                    gss_buffer_t exportedName,
591                                    OM_uint32 flags);
592 OM_uint32 gssEapImportName(OM_uint32 *minor,
593                            const gss_buffer_t input_name_buffer,
594                            const gss_OID input_name_type,
595                            const gss_OID input_mech_type,
596                            gss_name_t *output_name);
597 OM_uint32 gssEapImportNameInternal(OM_uint32 *minor,
598                                    const gss_buffer_t input_name_buffer,
599                                    gss_name_t *output_name,
600                                    OM_uint32 flags);
601 OM_uint32
602 gssEapDuplicateName(OM_uint32 *minor,
603                     const gss_name_t input_name,
604                     gss_name_t *dest_name);
605
606 OM_uint32
607 gssEapCanonicalizeName(OM_uint32 *minor,
608                        const gss_name_t input_name,
609                        const gss_OID mech_type,
610                        gss_name_t *dest_name);
611
612 OM_uint32
613 gssEapDisplayName(OM_uint32 *minor,
614                   gss_name_t name,
615                   gss_buffer_t output_name_buffer,
616                   gss_OID *output_name_type);
617
618 #define COMPARE_NAME_FLAG_IGNORE_EMPTY_REALMS   0x1
619
620 OM_uint32
621 gssEapCompareName(OM_uint32 *minor,
622                   gss_name_t name1,
623                   gss_name_t name2,
624                   OM_uint32 flags,
625                   int *name_equal);
626
627 /* util_oid.c */
628 OM_uint32
629 composeOid(OM_uint32 *minor_status,
630            const char *prefix,
631            size_t prefix_len,
632            int suffix,
633            gss_OID_desc *oid);
634
635 OM_uint32
636 decomposeOid(OM_uint32 *minor_status,
637              const char *prefix,
638              size_t prefix_len,
639              gss_OID_desc *oid,
640              int *suffix) ;
641
642 OM_uint32
643 duplicateOid(OM_uint32 *minor_status,
644              const gss_OID_desc * const oid,
645              gss_OID *new_oid);
646
647 OM_uint32
648 duplicateOidSet(OM_uint32 *minor,
649                 const gss_OID_set src,
650                 gss_OID_set *dst);
651
652 static inline int
653 oidEqual(const gss_OID_desc *o1, const gss_OID_desc *o2)
654 {
655     if (o1 == GSS_C_NO_OID)
656         return (o2 == GSS_C_NO_OID);
657     else if (o2 == GSS_C_NO_OID)
658         return (o1 == GSS_C_NO_OID);
659     else
660         return (o1->length == o2->length &&
661                 memcmp(o1->elements, o2->elements, o1->length) == 0);
662 }
663
664 /* util_ordering.c */
665 OM_uint32
666 sequenceInternalize(OM_uint32 *minor,
667                     void **vqueue,
668                     unsigned char **buf,
669                     size_t *lenremain);
670
671 OM_uint32
672 sequenceExternalize(OM_uint32 *minor,
673                     void *vqueue,
674                     unsigned char **buf,
675                     size_t *lenremain);
676
677 size_t
678 sequenceSize(void *vqueue);
679
680 OM_uint32
681 sequenceFree(OM_uint32 *minor, void **vqueue);
682
683 OM_uint32
684 sequenceCheck(OM_uint32 *minor, void **vqueue, uint64_t seqnum);
685
686 OM_uint32
687 sequenceInit(OM_uint32 *minor, void **vqueue, uint64_t seqnum,
688              int do_replay, int do_sequence, int wide_nums);
689
690 /* util_sm.c */
691 enum gss_eap_state {
692     GSSEAP_STATE_INITIAL        = 0x01,     /* initial state */
693     GSSEAP_STATE_AUTHENTICATE   = 0x02,     /* exchange EAP messages */
694     GSSEAP_STATE_INITIATOR_EXTS = 0x04,     /* initiator extensions */
695     GSSEAP_STATE_ACCEPTOR_EXTS  = 0x08,     /* acceptor extensions */
696 #ifdef GSSEAP_ENABLE_REAUTH
697     GSSEAP_STATE_REAUTHENTICATE = 0x10,     /* GSS reauthentication messages */
698 #endif
699     GSSEAP_STATE_ESTABLISHED    = 0x20,     /* context established */
700     GSSEAP_STATE_ALL            = 0x3F
701 };
702
703 #define GSSEAP_STATE_NEXT(s)    ((s) << 1)
704
705 #define GSSEAP_SM_STATE(ctx)                ((ctx)->state)
706
707 #ifdef GSSEAP_DEBUG
708 void gssEapSmTransition(gss_ctx_id_t ctx, enum gss_eap_state state);
709 #define GSSEAP_SM_TRANSITION(ctx, state)    gssEapSmTransition((ctx), (state))
710 #else
711 #define GSSEAP_SM_TRANSITION(ctx, newstate)    do { (ctx)->state = (newstate); } while (0)
712 #endif
713
714 #define GSSEAP_SM_TRANSITION_NEXT(ctx)      GSSEAP_SM_TRANSITION((ctx), GSSEAP_STATE_NEXT(GSSEAP_SM_STATE((ctx))))
715
716 /* state machine entry */
717 struct gss_eap_sm {
718     OM_uint32 inputTokenType;
719     OM_uint32 outputTokenType;
720     enum gss_eap_state validStates;
721     OM_uint32 itokFlags;
722     OM_uint32 (*processToken)(OM_uint32 *,
723                               gss_cred_id_t,
724                               gss_ctx_id_t,
725                               gss_name_t,
726                               gss_OID,
727                               OM_uint32,
728                               OM_uint32,
729                               gss_channel_bindings_t,
730                               gss_buffer_t,
731                               gss_buffer_t,
732                               OM_uint32 *);
733 };
734
735 /* state machine flags, set by handler */
736 #define SM_FLAG_FORCE_SEND_TOKEN            0x00000001  /* send token even if no inner tokens */
737 #define SM_FLAG_OUTPUT_TOKEN_CRITICAL       0x00000002  /* output token is critical */
738
739 /* state machine flags, set by state machine */
740 #define SM_FLAG_INPUT_TOKEN_CRITICAL        0x10000000  /* input token was critical */
741
742 #define SM_ITOK_FLAG_REQUIRED               0x00000001  /* received tokens must be present */
743
744 OM_uint32
745 gssEapSmStep(OM_uint32 *minor,
746              gss_cred_id_t cred,
747              gss_ctx_id_t ctx,
748              gss_name_t target,
749              gss_OID mech,
750              OM_uint32 reqFlags,
751              OM_uint32 timeReq,
752              gss_channel_bindings_t chanBindings,
753              gss_buffer_t inputToken,
754              gss_buffer_t outputToken,
755              struct gss_eap_sm *sm,
756              size_t smCount);
757
758 void
759 gssEapSmTransition(gss_ctx_id_t ctx, enum gss_eap_state state);
760
761 /* util_token.c */
762 struct gss_eap_token_buffer_set {
763     gss_buffer_set_desc buffers; /* pointers only */
764     OM_uint32 *types;
765 };
766
767 OM_uint32
768 gssEapEncodeInnerTokens(OM_uint32 *minor,
769                         struct gss_eap_token_buffer_set *tokens,
770                         gss_buffer_t buffer);
771 OM_uint32
772 gssEapDecodeInnerTokens(OM_uint32 *minor,
773                         const gss_buffer_t buffer,
774                         struct gss_eap_token_buffer_set *tokens);
775
776 OM_uint32
777 gssEapReleaseInnerTokens(OM_uint32 *minor,
778                          struct gss_eap_token_buffer_set *tokens,
779                          int freeBuffers);
780
781 OM_uint32
782 gssEapAllocInnerTokens(OM_uint32 *minor,
783                        size_t count,
784                        struct gss_eap_token_buffer_set *tokens);
785
786 size_t
787 tokenSize(const gss_OID_desc *mech, size_t body_size);
788
789 void
790 makeTokenHeader(const gss_OID_desc *mech,
791                 size_t body_size,
792                 unsigned char **buf,
793                 enum gss_eap_token_type tok_type);
794
795 OM_uint32
796 verifyTokenHeader(OM_uint32 *minor,
797                   gss_OID mech,
798                   size_t *body_size,
799                   unsigned char **buf_in,
800                   size_t toksize_in,
801                   enum gss_eap_token_type *ret_tok_type);
802
803 /* Helper macros */
804 #ifndef GSSEAP_MALLOC
805 #if _WIN32
806 #include <gssapi/gssapi_alloc.h>
807 #define GSSEAP_MALLOC                   gssalloc_malloc
808 #define GSSEAP_CALLOC                   gssalloc_calloc
809 #define GSSEAP_FREE                     gssalloc_free
810 #define GSSEAP_REALLOC                  gssalloc_realloc
811 #else
812 #define GSSEAP_CALLOC                   calloc
813 #define GSSEAP_MALLOC                   malloc
814 #define GSSEAP_FREE                     free
815 #define GSSEAP_REALLOC                  realloc
816 #endif /* _WIN32 */
817 #endif /* !GSSEAP_MALLOC */
818
819 #ifndef GSSAPI_CALLCONV
820 #define GSSAPI_CALLCONV                 KRB5_CALLCONV
821 #endif
822
823 #ifndef GSSEAP_ASSERT
824 #include <assert.h>
825 #define GSSEAP_ASSERT(x)                assert((x))
826 #endif /* !GSSEAP_ASSERT */
827
828 #ifdef WIN32
829 #define GSSEAP_CONSTRUCTOR
830 #define GSSEAP_DESTRUCTOR
831 #else
832 #define GSSEAP_CONSTRUCTOR              __attribute__((constructor))
833 #define GSSEAP_DESTRUCTOR               __attribute__((destructor))
834 #endif
835
836 #define GSSEAP_NOT_IMPLEMENTED          do {            \
837         GSSEAP_ASSERT(0 && "not implemented");          \
838         *minor = ENOSYS;                                \
839         return GSS_S_FAILURE;                           \
840     } while (0)
841
842 #ifdef WIN32
843
844 #include <winbase.h>
845
846 #define GSSEAP_GET_LAST_ERROR()         (GetLastError()) /* XXX FIXME */
847
848 #define GSSEAP_MUTEX                    CRITICAL_SECTION
849 #define GSSEAP_MUTEX_INIT(m)            (InitializeCriticalSection((m)), 0)
850 #define GSSEAP_MUTEX_DESTROY(m)         DeleteCriticalSection((m))
851 #define GSSEAP_MUTEX_LOCK(m)            EnterCriticalSection((m))
852 #define GSSEAP_MUTEX_UNLOCK(m)          LeaveCriticalSection((m))
853 #define GSSEAP_ONCE_LEAVE               do { return TRUE; } while (0)
854
855 /* Thread-local is handled separately */
856
857 #define GSSEAP_THREAD_ONCE              INIT_ONCE
858 #define GSSEAP_ONCE_CALLBACK(cb)        BOOL CALLBACK cb(PINIT_ONCE InitOnce, PVOID Parameter, PVOID *Context)
859 #define GSSEAP_ONCE(o, i)               InitOnceExecuteOnce((o), (i), NULL, NULL)
860 #define GSSEAP_ONCE_INITIALIZER         INIT_ONCE_STATIC_INIT
861
862 #else
863
864 #include <pthread.h>
865
866 #define GSSEAP_GET_LAST_ERROR()         (errno)
867
868 #define GSSEAP_MUTEX                    pthread_mutex_t
869 #define GSSEAP_MUTEX_INIT(m)            pthread_mutex_init((m), NULL)
870 #define GSSEAP_MUTEX_DESTROY(m)         pthread_mutex_destroy((m))
871 #define GSSEAP_MUTEX_LOCK(m)            pthread_mutex_lock((m))
872 #define GSSEAP_MUTEX_UNLOCK(m)          pthread_mutex_unlock((m))
873
874 #define GSSEAP_THREAD_KEY               pthread_key_t
875 #define GSSEAP_KEY_CREATE(k, d)         pthread_key_create((k), (d))
876 #define GSSEAP_GETSPECIFIC(k)           pthread_getspecific((k))
877 #define GSSEAP_SETSPECIFIC(k, d)        pthread_setspecific((k), (d))
878
879 #define GSSEAP_THREAD_ONCE              pthread_once_t
880 #define GSSEAP_ONCE_CALLBACK(cb)        void cb(void)
881 #define GSSEAP_ONCE(o, i)               pthread_once((o), (i))
882 #define GSSEAP_ONCE_INITIALIZER         PTHREAD_ONCE_INIT
883 #define GSSEAP_ONCE_LEAVE               do { } while (0)
884
885 #endif /* WIN32 */
886
887 /* Helper functions */
888 static inline void
889 store_uint16_be(uint16_t val, void *vp)
890 {
891     unsigned char *p = (unsigned char *)vp;
892
893     p[0] = (val >>  8) & 0xff;
894     p[1] = (val      ) & 0xff;
895 }
896
897 static inline uint16_t
898 load_uint16_be(const void *cvp)
899 {
900     const unsigned char *p = (const unsigned char *)cvp;
901
902     return (p[1] | (p[0] << 8));
903 }
904
905 static inline void
906 store_uint32_be(uint32_t val, void *vp)
907 {
908     unsigned char *p = (unsigned char *)vp;
909
910     p[0] = (val >> 24) & 0xff;
911     p[1] = (val >> 16) & 0xff;
912     p[2] = (val >>  8) & 0xff;
913     p[3] = (val      ) & 0xff;
914 }
915
916 static inline uint32_t
917 load_uint32_be(const void *cvp)
918 {
919     const unsigned char *p = (const unsigned char *)cvp;
920
921     return (p[3] | (p[2] << 8)
922             | ((uint32_t) p[1] << 16)
923             | ((uint32_t) p[0] << 24));
924 }
925
926 static inline void
927 store_uint64_be(uint64_t val, void *vp)
928 {
929     unsigned char *p = (unsigned char *)vp;
930
931     p[0] = (unsigned char)((val >> 56) & 0xff);
932     p[1] = (unsigned char)((val >> 48) & 0xff);
933     p[2] = (unsigned char)((val >> 40) & 0xff);
934     p[3] = (unsigned char)((val >> 32) & 0xff);
935     p[4] = (unsigned char)((val >> 24) & 0xff);
936     p[5] = (unsigned char)((val >> 16) & 0xff);
937     p[6] = (unsigned char)((val >>  8) & 0xff);
938     p[7] = (unsigned char)((val      ) & 0xff);
939 }
940
941 static inline uint64_t
942 load_uint64_be(const void *cvp)
943 {
944     const unsigned char *p = (const unsigned char *)cvp;
945
946     return ((uint64_t)load_uint32_be(p) << 32) | load_uint32_be(p + 4);
947 }
948
949 static inline unsigned char *
950 store_buffer(gss_buffer_t buffer, void *vp, int wide_nums)
951 {
952     unsigned char *p = (unsigned char *)vp;
953
954     if (wide_nums) {
955         store_uint64_be(buffer->length, p);
956         p += 8;
957     } else {
958         store_uint32_be(buffer->length, p);
959         p += 4;
960     }
961
962     if (buffer->value != NULL) {
963         memcpy(p, buffer->value, buffer->length);
964         p += buffer->length;
965     }
966
967     return p;
968 }
969
970 static inline unsigned char *
971 load_buffer(const void *cvp, size_t length, gss_buffer_t buffer)
972 {
973     buffer->length = 0;
974     buffer->value = GSSEAP_MALLOC(length);
975     if (buffer->value == NULL)
976         return NULL;
977     buffer->length = length;
978     memcpy(buffer->value, cvp, length);
979     return (unsigned char *)cvp + length;
980 }
981
982 static inline unsigned char *
983 store_oid(gss_OID oid, void *vp)
984 {
985     gss_buffer_desc buf;
986
987     if (oid != GSS_C_NO_OID) {
988         buf.length = oid->length;
989         buf.value = oid->elements;
990     } else {
991         buf.length = 0;
992         buf.value = NULL;
993     }
994
995     return store_buffer(&buf, vp, FALSE);
996 }
997
998 static inline void
999 krbDataToGssBuffer(krb5_data *data, gss_buffer_t buffer)
1000 {
1001     buffer->value = (void *)data->data;
1002     buffer->length = data->length;
1003 }
1004
1005 static inline void
1006 krbPrincComponentToGssBuffer(krb5_principal krbPrinc,
1007                              int index, gss_buffer_t buffer)
1008 {
1009     if (KRB_PRINC_LENGTH(krbPrinc) < index) {
1010         buffer->value = NULL;
1011         buffer->length = 0;
1012     } else {
1013 #ifdef HAVE_HEIMDAL_VERSION
1014         buffer->value = (void *)KRB_PRINC_NAME(krbPrinc)[index];
1015         buffer->length = strlen((char *)buffer->value);
1016 #else
1017         buffer->value = (void *)krb5_princ_component(NULL, krbPrinc, index)->data;
1018         buffer->length = krb5_princ_component(NULL, krbPrinc, index)->length;
1019 #endif /* HAVE_HEIMDAL_VERSION */
1020     }
1021 }
1022
1023 static inline krb5_error_code
1024 krbPrincUnparseServiceSpecifics(krb5_context krbContext, krb5_principal krbPrinc,
1025                                 gss_buffer_t nameBuf)
1026 {
1027     krb5_error_code result = 0;
1028     if (KRB_PRINC_LENGTH(krbPrinc) > 2) {
1029         /* Acceptor-Service-Specific */
1030         krb5_principal_data ssiPrinc = *krbPrinc;
1031         char *ssi;
1032
1033         KRB_PRINC_LENGTH(&ssiPrinc) -= 2;
1034         KRB_PRINC_NAME(&ssiPrinc) += 2;
1035
1036         result = krb5_unparse_name_flags(krbContext, &ssiPrinc,
1037                                          KRB5_PRINCIPAL_UNPARSE_NO_REALM, &ssi);
1038         if (result != 0)
1039             return result;
1040
1041         nameBuf->value = ssi;
1042         nameBuf->length = strlen(ssi);
1043     } else {
1044         nameBuf->value = NULL;
1045         nameBuf->length = 0;
1046     }
1047
1048     return result;
1049 }
1050
1051 static inline void
1052 krbFreeUnparsedName(krb5_context krbContext, gss_buffer_t nameBuf)
1053 {
1054     krb5_free_unparsed_name(krbContext, (char *)(nameBuf->value));
1055     nameBuf->value = NULL;
1056     nameBuf->length = 0;
1057 }
1058
1059 static inline void
1060 krbPrincRealmToGssBuffer(krb5_principal krbPrinc, gss_buffer_t buffer)
1061 {
1062 #ifdef HAVE_HEIMDAL_VERSION
1063     buffer->value = (void *)KRB_PRINC_REALM(krbPrinc);
1064     buffer->length = strlen((char *)buffer->value);
1065 #else
1066     krbDataToGssBuffer(KRB_PRINC_REALM(krbPrinc), buffer);
1067 #endif
1068 }
1069
1070 static inline void
1071 gssBufferToKrbData(gss_buffer_t buffer, krb5_data *data)
1072 {
1073     data->data = (char *)buffer->value;
1074     data->length = buffer->length;
1075 }
1076
1077 /* util_tld.c */
1078 struct gss_eap_status_info;
1079
1080 struct gss_eap_thread_local_data {
1081     krb5_context krbContext;
1082     struct gss_eap_status_info *statusInfo;
1083 };
1084
1085 struct gss_eap_thread_local_data *
1086 gssEapGetThreadLocalData(void);
1087
1088 void
1089 gssEapDestroyStatusInfo(struct gss_eap_status_info *status);
1090
1091 void
1092 gssEapDestroyKrbContext(krb5_context context);
1093
1094 #ifdef __cplusplus
1095 }
1096 #endif
1097
1098 #ifdef GSSEAP_ENABLE_ACCEPTOR
1099 #include "util_json.h"
1100 #include "util_attr.h"
1101 #include "util_base64.h"
1102 #endif /* GSSEAP_ENABLE_ACCEPTOR */
1103 #ifdef GSSEAP_ENABLE_REAUTH
1104 #include "util_reauth.h"
1105 #endif
1106
1107 #endif /* _UTIL_H_ */