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