1a51d6ffe1a5bde0815ab7ab425d3c7930d9533d
[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_error_code
341 krbCryptoLength(krb5_context krbContext,
342 #ifdef HAVE_HEIMDAL_VERSION
343                 krb5_crypto krbCrypto,
344 #else
345                 krb5_keyblock *key,
346 #endif
347                 int type,
348                 size_t *length);
349
350 krb5_error_code
351 krbPaddingLength(krb5_context krbContext,
352 #ifdef HAVE_HEIMDAL_VERSION
353                  krb5_crypto krbCrypto,
354 #else
355                  krb5_keyblock *key,
356 #endif
357                  size_t dataLength,
358                  size_t *padLength);
359
360 krb5_error_code
361 krbBlockSize(krb5_context krbContext,
362 #ifdef HAVE_HEIMDAL_VERSION
363                  krb5_crypto krbCrypto,
364 #else
365                  krb5_keyblock *key,
366 #endif
367                  size_t *blockSize);
368
369 krb5_error_code
370 krbEnctypeToString(krb5_context krbContext,
371                    krb5_enctype enctype,
372                    const char *prefix,
373                    gss_buffer_t string);
374
375 krb5_error_code
376 krbMakeAuthDataKdcIssued(krb5_context context,
377                          const krb5_keyblock *key,
378                          krb5_const_principal issuer,
379 #ifdef HAVE_HEIMDAL_VERSION
380                          const AuthorizationData *authdata,
381                          AuthorizationData *adKdcIssued
382 #else
383                          krb5_authdata *const *authdata,
384                          krb5_authdata ***adKdcIssued
385 #endif
386                          );
387
388 krb5_error_code
389 krbMakeCred(krb5_context context,
390             krb5_auth_context authcontext,
391             krb5_creds *creds,
392             krb5_data *data);
393
394 /* util_lucid.c */
395 OM_uint32
396 gssEapExportLucidSecContext(OM_uint32 *minor,
397                             gss_ctx_id_t ctx,
398                             const gss_OID desiredObject,
399                             gss_buffer_set_t *data_set);
400
401 /* util_mech.c */
402 extern gss_OID GSS_EAP_MECHANISM;
403
404 #define OID_FLAG_NULL_VALID                 0x00000001
405 #define OID_FLAG_FAMILY_MECH_VALID          0x00000002
406 #define OID_FLAG_MAP_NULL_TO_DEFAULT_MECH   0x00000004
407 #define OID_FLAG_MAP_FAMILY_MECH_TO_NULL    0x00000008
408
409 OM_uint32
410 gssEapCanonicalizeOid(OM_uint32 *minor,
411                       const gss_OID oid,
412                       OM_uint32 flags,
413                       gss_OID *pOid);
414
415 OM_uint32
416 gssEapReleaseOid(OM_uint32 *minor, gss_OID *oid);
417
418 OM_uint32
419 gssEapDefaultMech(OM_uint32 *minor,
420                   gss_OID *oid);
421
422 OM_uint32
423 gssEapIndicateMechs(OM_uint32 *minor,
424                     gss_OID_set *mechs);
425
426 OM_uint32
427 gssEapEnctypeToOid(OM_uint32 *minor,
428                    krb5_enctype enctype,
429                    gss_OID *pOid);
430
431 OM_uint32
432 gssEapOidToEnctype(OM_uint32 *minor,
433                    const gss_OID oid,
434                    krb5_enctype *enctype);
435
436 int
437 gssEapIsMechanismOid(const gss_OID oid);
438
439 int
440 gssEapIsConcreteMechanismOid(const gss_OID oid);
441
442 OM_uint32
443 gssEapValidateMechs(OM_uint32 *minor,
444                    const gss_OID_set mechs);
445
446 gss_buffer_t
447 gssEapOidToSaslName(const gss_OID oid);
448
449 gss_OID
450 gssEapSaslNameToOid(const gss_buffer_t name);
451
452 /* util_name.c */
453 #define EXPORT_NAME_FLAG_OID                    0x1
454 #define EXPORT_NAME_FLAG_COMPOSITE              0x2
455
456 OM_uint32 gssEapAllocName(OM_uint32 *minor, gss_name_t *pName);
457 OM_uint32 gssEapReleaseName(OM_uint32 *minor, gss_name_t *pName);
458 OM_uint32 gssEapExportName(OM_uint32 *minor,
459                            const gss_name_t name,
460                            gss_buffer_t exportedName);
461 OM_uint32 gssEapExportNameInternal(OM_uint32 *minor,
462                                    const gss_name_t name,
463                                    gss_buffer_t exportedName,
464                                    OM_uint32 flags);
465 OM_uint32 gssEapImportName(OM_uint32 *minor,
466                            const gss_buffer_t input_name_buffer,
467                            const gss_OID input_name_type,
468                            const gss_OID input_mech_type,
469                            gss_name_t *output_name);
470 OM_uint32 gssEapImportNameInternal(OM_uint32 *minor,
471                                    const gss_buffer_t input_name_buffer,
472                                    gss_name_t *output_name,
473                                    OM_uint32 flags);
474 OM_uint32
475 gssEapDuplicateName(OM_uint32 *minor,
476                     const gss_name_t input_name,
477                     gss_name_t *dest_name);
478
479 OM_uint32
480 gssEapCanonicalizeName(OM_uint32 *minor,
481                        const gss_name_t input_name,
482                        const gss_OID mech_type,
483                        gss_name_t *dest_name);
484
485 OM_uint32
486 gssEapDisplayName(OM_uint32 *minor,
487                   gss_name_t name,
488                   gss_buffer_t output_name_buffer,
489                   gss_OID *output_name_type);
490
491 OM_uint32
492 gssEapCompareName(OM_uint32 *minor,
493                   gss_name_t name1,
494                   gss_name_t name2,
495                   int *name_equal);
496
497 /* util_oid.c */
498 OM_uint32
499 composeOid(OM_uint32 *minor_status,
500            const char *prefix,
501            size_t prefix_len,
502            int suffix,
503            gss_OID_desc *oid);
504
505 OM_uint32
506 decomposeOid(OM_uint32 *minor_status,
507              const char *prefix,
508              size_t prefix_len,
509              gss_OID_desc *oid,
510              int *suffix) ;
511
512 OM_uint32
513 duplicateOid(OM_uint32 *minor_status,
514              const gss_OID_desc * const oid,
515              gss_OID *new_oid);
516
517 OM_uint32
518 duplicateOidSet(OM_uint32 *minor,
519                 const gss_OID_set src,
520                 gss_OID_set *dst);
521
522 static inline int
523 oidEqual(const gss_OID_desc *o1, const gss_OID_desc *o2)
524 {
525     if (o1 == GSS_C_NO_OID)
526         return (o2 == GSS_C_NO_OID);
527     else if (o2 == GSS_C_NO_OID)
528         return (o1 == GSS_C_NO_OID);
529     else
530         return (o1->length == o2->length &&
531                 memcmp(o1->elements, o2->elements, o1->length) == 0);
532 }
533
534 /* util_ordering.c */
535 OM_uint32
536 sequenceInternalize(OM_uint32 *minor,
537                     void **vqueue,
538                     unsigned char **buf,
539                     size_t *lenremain);
540
541 OM_uint32
542 sequenceExternalize(OM_uint32 *minor,
543                     void *vqueue,
544                     unsigned char **buf,
545                     size_t *lenremain);
546
547 size_t
548 sequenceSize(void *vqueue);
549
550 OM_uint32
551 sequenceFree(OM_uint32 *minor, void **vqueue);
552
553 OM_uint32
554 sequenceCheck(OM_uint32 *minor, void **vqueue, uint64_t seqnum);
555
556 OM_uint32
557 sequenceInit(OM_uint32 *minor, void **vqueue, uint64_t seqnum,
558              int do_replay, int do_sequence, int wide_nums);
559
560 /* util_sm.c */
561 enum gss_eap_state {
562     GSSEAP_STATE_INITIAL        = 0x01,     /* initial state */
563     GSSEAP_STATE_AUTHENTICATE   = 0x02,     /* exchange EAP messages */
564     GSSEAP_STATE_INITIATOR_EXTS = 0x04,     /* initiator extensions */
565     GSSEAP_STATE_ACCEPTOR_EXTS  = 0x08,     /* acceptor extensions */
566 #ifdef GSSEAP_ENABLE_REAUTH
567     GSSEAP_STATE_REAUTHENTICATE = 0x10,     /* GSS reauthentication messages */
568 #endif
569     GSSEAP_STATE_ESTABLISHED    = 0x20,     /* context established */
570     GSSEAP_STATE_ALL            = 0x3F
571 };
572
573 #define GSSEAP_STATE_NEXT(s)    ((s) << 1)
574
575 #define GSSEAP_SM_STATE(ctx)                ((ctx)->state)
576
577 #ifdef GSSEAP_DEBUG
578 void gssEapSmTransition(gss_ctx_id_t ctx, enum gss_eap_state state);
579 #define GSSEAP_SM_TRANSITION(ctx, state)    gssEapSmTransition((ctx), (state))
580 #else
581 #define GSSEAP_SM_TRANSITION(ctx, newstate)    do { (ctx)->state = (newstate); } while (0)
582 #endif
583
584 #define GSSEAP_SM_TRANSITION_NEXT(ctx)      GSSEAP_SM_TRANSITION((ctx), GSSEAP_STATE_NEXT(GSSEAP_SM_STATE((ctx))))
585
586 /* state machine entry */
587 struct gss_eap_sm {
588     OM_uint32 inputTokenType;
589     OM_uint32 outputTokenType;
590     enum gss_eap_state validStates;
591     OM_uint32 itokFlags;
592     OM_uint32 (*processToken)(OM_uint32 *,
593                               gss_cred_id_t,
594                               gss_ctx_id_t,
595                               gss_name_t,
596                               gss_OID,
597                               OM_uint32,
598                               OM_uint32,
599                               gss_channel_bindings_t,
600                               gss_buffer_t,
601                               gss_buffer_t,
602                               OM_uint32 *);
603 };
604
605 /* state machine flags, set by handler */
606 #define SM_FLAG_FORCE_SEND_TOKEN            0x00000001  /* send token even if no inner tokens */
607 #define SM_FLAG_OUTPUT_TOKEN_CRITICAL       0x00000002  /* output token is critical */
608
609 /* state machine flags, set by state machine */
610 #define SM_FLAG_INPUT_TOKEN_CRITICAL        0x10000000  /* input token was critical */
611
612 #define SM_ITOK_FLAG_REQUIRED               0x00000001  /* received tokens must be present */
613
614 OM_uint32
615 gssEapSmStep(OM_uint32 *minor,
616              gss_cred_id_t cred,
617              gss_ctx_id_t ctx,
618              gss_name_t target,
619              gss_OID mech,
620              OM_uint32 reqFlags,
621              OM_uint32 timeReq,
622              gss_channel_bindings_t chanBindings,
623              gss_buffer_t inputToken,
624              gss_buffer_t outputToken,
625              struct gss_eap_sm *sm,
626              size_t smCount);
627
628 void
629 gssEapSmTransition(gss_ctx_id_t ctx, enum gss_eap_state state);
630
631 /* util_token.c */
632 OM_uint32
633 gssEapEncodeInnerTokens(OM_uint32 *minor,
634                         gss_buffer_set_t extensions,
635                         OM_uint32 *types,
636                         gss_buffer_t buffer);
637 OM_uint32
638 gssEapDecodeInnerTokens(OM_uint32 *minor,
639                         const gss_buffer_t buffer,
640                         gss_buffer_set_t *pExtensions,
641                         OM_uint32 **pTypes);
642
643 size_t
644 tokenSize(const gss_OID_desc *mech, size_t body_size);
645
646 void
647 makeTokenHeader(const gss_OID_desc *mech,
648                 size_t body_size,
649                 unsigned char **buf,
650                 enum gss_eap_token_type tok_type);
651
652 OM_uint32
653 verifyTokenHeader(OM_uint32 *minor,
654                   gss_OID mech,
655                   size_t *body_size,
656                   unsigned char **buf_in,
657                   size_t toksize_in,
658                   enum gss_eap_token_type *ret_tok_type);
659
660 /* Helper macros */
661
662 #define GSSEAP_CALLOC                   calloc
663 #define GSSEAP_MALLOC                   malloc
664 #define GSSEAP_FREE                     free
665 #define GSSEAP_REALLOC                  realloc
666
667 #define GSSEAP_NOT_IMPLEMENTED          do {            \
668         assert(0 && "not implemented");                 \
669         *minor = ENOSYS;                                \
670         return GSS_S_FAILURE;                           \
671     } while (0)
672
673 #include <pthread.h>
674
675 #define GSSEAP_MUTEX                    pthread_mutex_t
676 #define GSSEAP_MUTEX_INITIALIZER        PTHREAD_MUTEX_INITIALIZER
677
678 #define GSSEAP_MUTEX_INIT(m)            pthread_mutex_init((m), NULL)
679 #define GSSEAP_MUTEX_DESTROY(m)         pthread_mutex_destroy((m))
680 #define GSSEAP_MUTEX_LOCK(m)            pthread_mutex_lock((m))
681 #define GSSEAP_MUTEX_UNLOCK(m)          pthread_mutex_unlock((m))
682
683 #define GSSEAP_THREAD_KEY               pthread_key_t
684 #define GSSEAP_KEY_CREATE(k, d)         pthread_key_create((k), (d))
685 #define GSSEAP_GETSPECIFIC(k)           pthread_getspecific((k))
686 #define GSSEAP_SETSPECIFIC(k, d)        pthread_setspecific((k), (d))
687
688 #define GSSEAP_THREAD_ONCE              pthread_once_t
689 #define GSSEAP_ONCE(o, i)               pthread_once((o), (i))
690 #define GSSEAP_ONCE_INITIALIZER         PTHREAD_ONCE_INIT
691
692 /* Helper functions */
693 static inline void
694 store_uint16_be(uint16_t val, void *vp)
695 {
696     unsigned char *p = (unsigned char *)vp;
697
698     p[0] = (val >>  8) & 0xff;
699     p[1] = (val      ) & 0xff;
700 }
701
702 static inline uint16_t
703 load_uint16_be(const void *cvp)
704 {
705     const unsigned char *p = (const unsigned char *)cvp;
706
707     return (p[1] | (p[0] << 8));
708 }
709
710 static inline void
711 store_uint32_be(uint32_t val, void *vp)
712 {
713     unsigned char *p = (unsigned char *)vp;
714
715     p[0] = (val >> 24) & 0xff;
716     p[1] = (val >> 16) & 0xff;
717     p[2] = (val >>  8) & 0xff;
718     p[3] = (val      ) & 0xff;
719 }
720
721 static inline uint32_t
722 load_uint32_be(const void *cvp)
723 {
724     const unsigned char *p = (const unsigned char *)cvp;
725
726     return (p[3] | (p[2] << 8)
727             | ((uint32_t) p[1] << 16)
728             | ((uint32_t) p[0] << 24));
729 }
730
731 static inline void
732 store_uint64_be(uint64_t val, void *vp)
733 {
734     unsigned char *p = (unsigned char *)vp;
735
736     p[0] = (unsigned char)((val >> 56) & 0xff);
737     p[1] = (unsigned char)((val >> 48) & 0xff);
738     p[2] = (unsigned char)((val >> 40) & 0xff);
739     p[3] = (unsigned char)((val >> 32) & 0xff);
740     p[4] = (unsigned char)((val >> 24) & 0xff);
741     p[5] = (unsigned char)((val >> 16) & 0xff);
742     p[6] = (unsigned char)((val >>  8) & 0xff);
743     p[7] = (unsigned char)((val      ) & 0xff);
744 }
745
746 static inline uint64_t
747 load_uint64_be(const void *cvp)
748 {
749     const unsigned char *p = (const unsigned char *)cvp;
750
751     return ((uint64_t)load_uint32_be(p) << 32) | load_uint32_be(p + 4);
752 }
753
754 static inline unsigned char *
755 store_buffer(gss_buffer_t buffer, void *vp, int wide_nums)
756 {
757     unsigned char *p = (unsigned char *)vp;
758
759     if (wide_nums) {
760         store_uint64_be(buffer->length, p);
761         p += 8;
762     } else {
763         store_uint32_be(buffer->length, p);
764         p += 4;
765     }
766
767     if (buffer->value != NULL) {
768         memcpy(p, buffer->value, buffer->length);
769         p += buffer->length;
770     }
771
772     return p;
773 }
774
775 static inline unsigned char *
776 load_buffer(const void *cvp, size_t length, gss_buffer_t buffer)
777 {
778     buffer->length = 0;
779     buffer->value = GSSEAP_MALLOC(length);
780     if (buffer->value == NULL)
781         return NULL;
782     buffer->length = length;
783     memcpy(buffer->value, cvp, length);
784     return (unsigned char *)cvp + length;
785 }
786
787 static inline unsigned char *
788 store_oid(gss_OID oid, void *vp)
789 {
790     gss_buffer_desc buf;
791
792     if (oid != GSS_C_NO_OID) {
793         buf.length = oid->length;
794         buf.value = oid->elements;
795     } else {
796         buf.length = 0;
797         buf.value = NULL;
798     }
799
800     return store_buffer(&buf, vp, FALSE);
801 }
802
803 static inline void
804 krbDataToGssBuffer(krb5_data *data, gss_buffer_t buffer)
805 {
806     buffer->value = (void *)data->data;
807     buffer->length = data->length;
808 }
809
810 static inline void
811 krbPrincComponentToGssBuffer(krb5_principal krbPrinc,
812                              int index, gss_buffer_t buffer)
813 {
814 #ifdef HAVE_HEIMDAL_VERSION
815     buffer->value = (void *)KRB_PRINC_NAME(krbPrinc)[index];
816     buffer->length = strlen((char *)buffer->value);
817 #else
818     buffer->value = (void *)krb5_princ_component(NULL, krbPrinc, index)->data;
819     buffer->length = krb5_princ_component(NULL, krbPrinc, index)->length;
820 #endif /* HAVE_HEIMDAL_VERSION */
821 }
822
823 static inline void
824 krbPrincRealmToGssBuffer(krb5_principal krbPrinc, gss_buffer_t buffer)
825 {
826 #ifdef HAVE_HEIMDAL_VERSION
827     buffer->value = (void *)KRB_PRINC_REALM(krbPrinc);
828     buffer->length = strlen((char *)buffer->value);
829 #else
830     krbDataToGssBuffer(KRB_PRINC_REALM(krbPrinc), buffer);
831 #endif
832 }
833
834 static inline void
835 gssBufferToKrbData(gss_buffer_t buffer, krb5_data *data)
836 {
837     data->data = (char *)buffer->value;
838     data->length = buffer->length;
839 }
840
841 #ifdef __cplusplus
842 }
843 #endif
844
845 #include "util_json.h"
846 #include "util_attr.h"
847 #include "util_base64.h"
848 #ifdef GSSEAP_ENABLE_REAUTH
849 #include "util_reauth.h"
850 #endif
851
852 #endif /* _UTIL_H_ */