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