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