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