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