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