Heimdal portability fixes (except for reauth)
[mech_eap.orig] / util.h
1 /*
2  * Copyright (c) 2010, 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_EAP_RESP                = 0x0601,  /* EAP response */
158     TOK_TYPE_EAP_REQ                 = 0x0602,  /* EAP request */
159     TOK_TYPE_EXT_REQ                 = 0x0603,  /* GSS EAP extensions request */
160     TOK_TYPE_EXT_RESP                = 0x0604,  /* GSS EAP extensions response */
161     TOK_TYPE_GSS_REAUTH              = 0x0605,  /* GSS EAP fast reauthentication token */
162     TOK_TYPE_CONTEXT_ERR             = 0x0606,  /* context error */
163 };
164
165 OM_uint32 gssEapAllocContext(OM_uint32 *minor, gss_ctx_id_t *pCtx);
166 OM_uint32 gssEapReleaseContext(OM_uint32 *minor, gss_ctx_id_t *pCtx);
167
168 OM_uint32
169 gssEapMakeToken(OM_uint32 *minor,
170                 gss_ctx_id_t ctx,
171                 const gss_buffer_t innerToken,
172                 enum gss_eap_token_type tokenType,
173                 gss_buffer_t outputToken);
174
175 OM_uint32
176 gssEapVerifyToken(OM_uint32 *minor,
177                   gss_ctx_id_t ctx,
178                   const gss_buffer_t inputToken,
179                   enum gss_eap_token_type *tokenType,
180                   gss_buffer_t innerInputToken);
181
182 OM_uint32
183 gssEapContextTime(OM_uint32 *minor,
184                   gss_ctx_id_t context_handle,
185                   OM_uint32 *time_rec);
186
187 OM_uint32
188 gssEapDisplayName(OM_uint32 *minor,
189                   gss_name_t name,
190                   gss_buffer_t output_name_buffer,
191                   gss_OID *output_name_type);
192
193 /* util_cred.c */
194 OM_uint32 gssEapAllocCred(OM_uint32 *minor, gss_cred_id_t *pCred);
195 OM_uint32 gssEapReleaseCred(OM_uint32 *minor, gss_cred_id_t *pCred);
196
197 OM_uint32
198 gssEapAcquireCred(OM_uint32 *minor,
199                   const gss_name_t desiredName,
200                   const gss_buffer_t password,
201                   OM_uint32 timeReq,
202                   const gss_OID_set desiredMechs,
203                   int cred_usage,
204                   gss_cred_id_t *pCred,
205                   gss_OID_set *pActualMechs,
206                   OM_uint32 *timeRec);
207
208 int gssEapCredAvailable(gss_cred_id_t cred, gss_OID mech);
209
210 /* util_crypt.c */
211 int
212 gssEapEncrypt(krb5_context context, int dce_style, size_t ec,
213               size_t rrc,
214 #ifdef HAVE_HEIMDAL_VERSION
215               krb5_crypto crypto,
216 #else
217               krb5_keyblock *key,
218 #endif
219               int usage,
220               gss_iov_buffer_desc *iov, int iov_count);
221
222 int
223 gssEapDecrypt(krb5_context context, int dce_style, size_t ec,
224               size_t rrc,
225 #ifdef HAVE_HEIMDAL_VERSION
226               krb5_crypto crypto,
227 #else
228               krb5_keyblock *key,
229 #endif
230               int usage,
231               gss_iov_buffer_desc *iov, int iov_count);
232
233 int
234 gssEapMapCryptoFlag(OM_uint32 type);
235
236 gss_iov_buffer_t
237 gssEapLocateIov(gss_iov_buffer_desc *iov,
238                 int iov_count,
239                 OM_uint32 type);
240
241 void
242 gssEapIovMessageLength(gss_iov_buffer_desc *iov,
243                        int iov_count,
244                        size_t *data_length,
245                        size_t *assoc_data_length);
246
247 void
248 gssEapReleaseIov(gss_iov_buffer_desc *iov, int iov_count);
249
250 int
251 gssEapIsIntegrityOnly(gss_iov_buffer_desc *iov, int iov_count);
252
253 int
254 gssEapAllocIov(gss_iov_buffer_t iov, size_t size);
255
256 OM_uint32
257 gssEapDeriveRfc3961Key(OM_uint32 *minor,
258                        const unsigned char *key,
259                        size_t keyLength,
260                        krb5_enctype enctype,
261                        krb5_keyblock *pKey);
262
263 /* util_exts.c */
264 #define EXT_FLAG_CRITICAL               0x80000000  /* critical, wire flag */
265 #define EXT_FLAG_VERIFIED               0x40000000  /* verified, API flag */
266
267 #define EXT_TYPE_GSS_CHANNEL_BINDINGS   0x00000000
268 #define EXT_TYPE_REAUTH_CREDS           0x00000001
269 #define EXT_TYPE_MASK                   (~(EXT_FLAG_CRITICAL | EXT_FLAG_VERIFIED))
270
271 struct gss_eap_extension_provider {
272     OM_uint32 type;
273     int critical; /* client */
274     int required; /* server */
275     OM_uint32 (*make)(OM_uint32 *,
276                       gss_cred_id_t,
277                       gss_ctx_id_t,
278                       gss_channel_bindings_t,
279                       gss_buffer_t);
280     OM_uint32 (*verify)(OM_uint32 *,
281                         gss_cred_id_t,
282                         gss_ctx_id_t,
283                         gss_channel_bindings_t,
284                         const gss_buffer_t);
285 };
286
287 OM_uint32
288 gssEapMakeExtensions(OM_uint32 *minor,
289                      gss_cred_id_t cred,
290                      gss_ctx_id_t ctx,
291                      gss_channel_bindings_t chanBindings,
292                      gss_buffer_t buffer);
293
294 OM_uint32
295 gssEapVerifyExtensions(OM_uint32 *minor,
296                        gss_cred_id_t cred,
297                        gss_ctx_id_t ctx,
298                        gss_channel_bindings_t chanBindings,
299                        const gss_buffer_t buffer);
300
301 /* util_krb.c */
302 #ifdef HAVE_HEIMDAL_VERSION
303 #define KRB_KEY_TYPE(key)       ((key)->keytype)
304 #define KRB_KEY_DATA(key)       ((key)->keyvalue.data)
305 #define KRB_KEY_LENGTH(key)     ((key)->keyvalue.length)
306 #else
307 #define KRB_KEY_TYPE(key)       ((key)->enctype)
308 #define KRB_KEY_DATA(key)       ((key)->contents)
309 #define KRB_KEY_LENGTH(key)     ((key)->length)
310 #endif /* HAVE_HEIMDAL_VERSION */
311
312 #define KRB_KEY_INIT(key)       do {        \
313         KRB_KEY_TYPE(key) = ENCTYPE_NULL;   \
314         KRB_KEY_DATA(key) = NULL;           \
315         KRB_KEY_LENGTH(key) = 0;            \
316     } while (0)
317
318 #ifdef HAVE_HEIMDAL_VERSION
319 #define KRB_PRINC_LENGTH(princ) ((princ)->name.name_string.len)
320 #define KRB_PRINC_TYPE(princ)   ((princ)->name.name_type)
321 #define KRB_PRINC_NAME(princ)   ((princ)->name.name_string.val)
322 #define KRB_CRYPTO_CONTEXT(ctx) (krbCrypto)
323 #else
324 #define KRB_PRINC_LENGTH(princ) (krb5_princ_size(NULL, (princ)))
325 #define KRB_PRINC_TYPE(princ)   (krb5_princ_type(NULL, (princ)))
326 #define KRB_PRINC_NAME(princ)   (krb5_princ_name(NULL, (princ)))
327 #define KRB_CRYPTO_CONTEXT(ctx) (&(ctx)->rfc3961Key)
328 #endif /* HAVE_HEIMDAL_VERSION */
329
330 #ifdef HAVE_HEIMDAL_VERSION
331 #define GSS_IOV_BUFFER_FLAG_ALLOCATE    GSS_IOV_BUFFER_TYPE_FLAG_ALLOCATE
332 #define GSS_IOV_BUFFER_FLAG_ALLOCATED   GSS_IOV_BUFFER_TYPE_FLAG_ALLOCATED
333
334 #define GSS_S_CRED_UNAVAIL              GSS_S_FAILURE
335 #endif
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 /* util_lucid.c */
393 OM_uint32
394 gssEapExportLucidSecContext(OM_uint32 *minor,
395                             gss_ctx_id_t ctx,
396                             const gss_OID desiredObject,
397                             gss_buffer_set_t *data_set);
398
399 /* util_mech.c */
400 extern gss_OID GSS_EAP_MECHANISM;
401
402 int
403 gssEapInternalizeOid(const gss_OID oid,
404                      gss_OID *const pInternalizedOid);
405
406 OM_uint32
407 gssEapDefaultMech(OM_uint32 *minor,
408                   gss_OID *oid);
409
410 OM_uint32
411 gssEapIndicateMechs(OM_uint32 *minor,
412                     gss_OID_set *mechs);
413
414 OM_uint32
415 gssEapEnctypeToOid(OM_uint32 *minor,
416                    krb5_enctype enctype,
417                    gss_OID *pOid);
418
419 OM_uint32
420 gssEapOidToEnctype(OM_uint32 *minor,
421                    const gss_OID oid,
422                    krb5_enctype *enctype);
423
424 int
425 gssEapIsMechanismOid(const gss_OID oid);
426
427 int
428 gssEapIsConcreteMechanismOid(const gss_OID oid);
429
430 OM_uint32
431 gssEapValidateMechs(OM_uint32 *minor,
432                    const gss_OID_set mechs);
433
434 gss_buffer_t
435 gssEapOidToSaslName(const gss_OID oid);
436
437 gss_OID
438 gssEapSaslNameToOid(const gss_buffer_t name);
439
440 /* util_name.c */
441 #define EXPORT_NAME_FLAG_OID        0x1
442 #define EXPORT_NAME_FLAG_COMPOSITE  0x2
443
444 OM_uint32 gssEapAllocName(OM_uint32 *minor, gss_name_t *pName);
445 OM_uint32 gssEapReleaseName(OM_uint32 *minor, gss_name_t *pName);
446 OM_uint32 gssEapExportName(OM_uint32 *minor,
447                            const gss_name_t name,
448                            gss_buffer_t exportedName);
449 OM_uint32 gssEapExportNameInternal(OM_uint32 *minor,
450                                    const gss_name_t name,
451                                    gss_buffer_t exportedName,
452                                    unsigned int flags);
453 OM_uint32 gssEapImportName(OM_uint32 *minor,
454                            const gss_buffer_t input_name_buffer,
455                            gss_OID input_name_type,
456                            gss_name_t *output_name);
457 OM_uint32 gssEapImportNameInternal(OM_uint32 *minor,
458                                    const gss_buffer_t input_name_buffer,
459                                    gss_name_t *output_name,
460                                    unsigned int flags);
461 OM_uint32
462 gssEapDuplicateName(OM_uint32 *minor,
463                     const gss_name_t input_name,
464                     gss_name_t *dest_name);
465
466 /* util_oid.c */
467 OM_uint32
468 composeOid(OM_uint32 *minor_status,
469            const char *prefix,
470            size_t prefix_len,
471            int suffix,
472            gss_OID_desc *oid);
473
474 OM_uint32
475 decomposeOid(OM_uint32 *minor_status,
476              const char *prefix,
477              size_t prefix_len,
478              gss_OID_desc *oid,
479              int *suffix) ;
480
481 OM_uint32
482 duplicateOid(OM_uint32 *minor_status,
483              const gss_OID_desc * const oid,
484              gss_OID *new_oid);
485
486 OM_uint32
487 duplicateOidSet(OM_uint32 *minor,
488                 const gss_OID_set src,
489                 gss_OID_set *dst);
490
491 static inline int
492 oidEqual(const gss_OID_desc *o1, const gss_OID_desc *o2)
493 {
494     if (o1 == GSS_C_NO_OID)
495         return (o2 == GSS_C_NO_OID);
496     else if (o2 == GSS_C_NO_OID)
497         return (o1 == GSS_C_NO_OID);
498     else
499         return (o1->length == o2->length &&
500                 memcmp(o1->elements, o2->elements, o1->length) == 0);
501 }
502
503 /* util_ordering.c */
504 OM_uint32
505 sequenceInternalize(OM_uint32 *minor,
506                     void **vqueue,
507                     unsigned char **buf,
508                     size_t *lenremain);
509
510 OM_uint32
511 sequenceExternalize(OM_uint32 *minor,
512                     void *vqueue,
513                     unsigned char **buf,
514                     size_t *lenremain);
515
516 size_t
517 sequenceSize(void *vqueue);
518
519 OM_uint32
520 sequenceFree(OM_uint32 *minor, void **vqueue);
521
522 OM_uint32
523 sequenceCheck(OM_uint32 *minor, void **vqueue, uint64_t seqnum);
524
525 OM_uint32
526 sequenceInit(OM_uint32 *minor, void **vqueue, uint64_t seqnum,
527              int do_replay, int do_sequence, int wide_nums);
528
529 /* util_token.c */
530 size_t
531 tokenSize(const gss_OID_desc *mech, size_t body_size);
532
533 void
534 makeTokenHeader(const gss_OID_desc *mech,
535                 size_t body_size,
536                 unsigned char **buf,
537                 enum gss_eap_token_type tok_type);
538
539 OM_uint32
540 verifyTokenHeader(OM_uint32 *minor,
541                   gss_OID mech,
542                   size_t *body_size,
543                   unsigned char **buf_in,
544                   size_t toksize_in,
545                   enum gss_eap_token_type *ret_tok_type);
546
547 /* Helper macros */
548
549 #define GSSEAP_CALLOC                   calloc
550 #define GSSEAP_MALLOC                   malloc
551 #define GSSEAP_FREE                     free
552 #define GSSEAP_REALLOC                  realloc
553
554 #define GSSEAP_NOT_IMPLEMENTED          do {            \
555         assert(0 && "not implemented");                 \
556         *minor = ENOSYS;                                \
557         return GSS_S_FAILURE;                           \
558     } while (0)
559
560 #include <pthread.h>
561
562 #define GSSEAP_MUTEX                    pthread_mutex_t
563 #define GSSEAP_MUTEX_INITIALIZER        PTHREAD_MUTEX_INITIALIZER
564
565 #define GSSEAP_MUTEX_INIT(m)            pthread_mutex_init((m), NULL)
566 #define GSSEAP_MUTEX_DESTROY(m)         pthread_mutex_destroy((m))
567 #define GSSEAP_MUTEX_LOCK(m)            pthread_mutex_lock((m))
568 #define GSSEAP_MUTEX_UNLOCK(m)          pthread_mutex_unlock((m))
569
570 #define GSSEAP_THREAD_KEY               pthread_key_t
571 #define GSSEAP_KEY_CREATE(k, d)         pthread_key_create((k), (d))
572 #define GSSEAP_GETSPECIFIC(k)           pthread_getspecific((k))
573 #define GSSEAP_SETSPECIFIC(k, d)        pthread_setspecific((k), (d))
574
575 #define GSSEAP_THREAD_ONCE              pthread_once_t
576 #define GSSEAP_ONCE(o, i)               pthread_once((o), (i))
577 #define GSSEAP_ONCE_INITIALIZER         PTHREAD_ONCE_INIT
578
579 /* Helper functions */
580 static inline void
581 store_uint16_be(uint16_t val, void *vp)
582 {
583     unsigned char *p = (unsigned char *)vp;
584
585     p[0] = (val >>  8) & 0xff;
586     p[1] = (val      ) & 0xff;
587 }
588
589 static inline uint16_t
590 load_uint16_be(const void *cvp)
591 {
592     const unsigned char *p = (const unsigned char *)cvp;
593
594     return (p[1] | (p[0] << 8));
595 }
596
597 static inline void
598 store_uint32_be(uint32_t val, void *vp)
599 {
600     unsigned char *p = (unsigned char *)vp;
601
602     p[0] = (val >> 24) & 0xff;
603     p[1] = (val >> 16) & 0xff;
604     p[2] = (val >>  8) & 0xff;
605     p[3] = (val      ) & 0xff;
606 }
607
608 static inline uint32_t
609 load_uint32_be(const void *cvp)
610 {
611     const unsigned char *p = (const unsigned char *)cvp;
612
613     return (p[3] | (p[2] << 8)
614             | ((uint32_t) p[1] << 16)
615             | ((uint32_t) p[0] << 24));
616 }
617
618 static inline void
619 store_uint64_be(uint64_t val, void *vp)
620 {
621     unsigned char *p = (unsigned char *)vp;
622
623     p[0] = (unsigned char)((val >> 56) & 0xff);
624     p[1] = (unsigned char)((val >> 48) & 0xff);
625     p[2] = (unsigned char)((val >> 40) & 0xff);
626     p[3] = (unsigned char)((val >> 32) & 0xff);
627     p[4] = (unsigned char)((val >> 24) & 0xff);
628     p[5] = (unsigned char)((val >> 16) & 0xff);
629     p[6] = (unsigned char)((val >>  8) & 0xff);
630     p[7] = (unsigned char)((val      ) & 0xff);
631 }
632
633 static inline uint64_t
634 load_uint64_be(const void *cvp)
635 {
636     const unsigned char *p = (const unsigned char *)cvp;
637
638     return ((uint64_t)load_uint32_be(p) << 32) | load_uint32_be(p + 4);
639 }
640
641 static inline unsigned char *
642 store_buffer(gss_buffer_t buffer, void *vp, int wide_nums)
643 {
644     unsigned char *p = (unsigned char *)vp;
645
646     if (wide_nums) {
647         store_uint64_be(buffer->length, p);
648         p += 8;
649     } else {
650         store_uint32_be(buffer->length, p);
651         p += 4;
652     }
653
654     if (buffer->value != NULL) {
655         memcpy(p, buffer->value, buffer->length);
656         p += buffer->length;
657     }
658
659     return p;
660 }
661
662 static inline unsigned char *
663 load_buffer(const void *cvp, size_t length, gss_buffer_t buffer)
664 {
665     buffer->length = 0;
666     buffer->value = GSSEAP_MALLOC(length);
667     if (buffer->value == NULL)
668         return NULL;
669     buffer->length = length;
670     memcpy(buffer->value, cvp, length);
671     return (unsigned char *)cvp + length;
672 }
673
674 static inline unsigned char *
675 store_oid(gss_OID oid, void *vp)
676 {
677     gss_buffer_desc buf;
678
679     if (oid != GSS_C_NO_OID) {
680         buf.length = oid->length;
681         buf.value = oid->elements;
682     } else {
683         buf.length = 0;
684         buf.value = NULL;
685     }
686
687     return store_buffer(&buf, vp, FALSE);
688 }
689
690 static inline void
691 krbDataToGssBuffer(krb5_data *data, gss_buffer_t buffer)
692 {
693     buffer->value = (void *)data->data;
694     buffer->length = data->length;
695 }
696
697 static inline void
698 krbPrincComponentToGssBuffer(krb5_principal krbPrinc,
699                              int index, gss_buffer_t buffer)
700 {
701 #ifdef HAVE_HEIMDAL_VERSION
702     buffer->value = (void *)krbPrinc->name.name_string.val[index];
703     buffer->length = strlen((char *)buffer->value);
704 #else
705     buffer->value = (void *)krb5_princ_component(NULL, krbPrinc, index)->data;
706     buffer->length = krb5_princ_component(NULL, krbPrinc, index)->length;
707 #endif /* HAVE_HEIMDAL_VERSION */
708 }
709
710 static inline void
711 krbPrincRealmToGssBuffer(krb5_principal krbPrinc, gss_buffer_t buffer)
712 {
713 #ifdef HAVE_HEIMDAL_VERSION
714     buffer->value = (void *)krbPrinc->realm;
715     buffer->length = strlen(krbPrinc->realm);
716 #else
717     krbDataToGssBuffer(krb5_princ_realm(NULL, krbPrinc), buffer);
718 #endif
719 }
720
721 static inline void
722 gssBufferToKrbData(gss_buffer_t buffer, krb5_data *data)
723 {
724     data->data = (char *)buffer->value;
725     data->length = buffer->length;
726 }
727
728 #ifdef __cplusplus
729 }
730 #endif
731
732 #include "util_attr.h"
733 #ifdef GSSEAP_ENABLE_REAUTH
734 #include "util_reauth.h"
735 #endif
736
737 #endif /* _UTIL_H_ */