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