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