More Heimdal reauth portability
[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_TIME_FOREVER        ((time_t)~0L)
304 #define KRB_KEY_TYPE(key)       ((key)->keytype)
305 #define KRB_KEY_DATA(key)       ((key)->keyvalue.data)
306 #define KRB_KEY_LENGTH(key)     ((key)->keyvalue.length)
307 #else
308 #define KRB_TIME_FOREVER        KRB5_INT32_MAX
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 #endif /* HAVE_HEIMDAL_VERSION */
313
314 #define KRB_KEY_INIT(key)       do {        \
315         KRB_KEY_TYPE(key) = ENCTYPE_NULL;   \
316         KRB_KEY_DATA(key) = NULL;           \
317         KRB_KEY_LENGTH(key) = 0;            \
318     } while (0)
319
320 #ifdef HAVE_HEIMDAL_VERSION
321 #define KRB_PRINC_LENGTH(princ) ((princ)->name.name_string.len)
322 #define KRB_PRINC_TYPE(princ)   ((princ)->name.name_type)
323 #define KRB_PRINC_NAME(princ)   ((princ)->name.name_string.val)
324 #define KRB_PRINC_REALM(princ)  ((princ)->realm)
325 #define KRB_CRYPTO_CONTEXT(ctx) (krbCrypto)
326 #else
327 #define KRB_PRINC_LENGTH(princ) (krb5_princ_size(NULL, (princ)))
328 #define KRB_PRINC_TYPE(princ)   (krb5_princ_type(NULL, (princ)))
329 #define KRB_PRINC_NAME(princ)   (krb5_princ_name(NULL, (princ)))
330 #define KRB_PRINC_REALM(princ)  (krb5_princ_realm(NULL, (princ)))
331 #define KRB_CRYPTO_CONTEXT(ctx) (&(ctx)->rfc3961Key)
332 #endif /* HAVE_HEIMDAL_VERSION */
333
334 #ifdef HAVE_HEIMDAL_VERSION
335 #define GSS_IOV_BUFFER_FLAG_ALLOCATE    GSS_IOV_BUFFER_TYPE_FLAG_ALLOCATE
336 #define GSS_IOV_BUFFER_FLAG_ALLOCATED   GSS_IOV_BUFFER_TYPE_FLAG_ALLOCATED
337
338 #define GSS_S_CRED_UNAVAIL              GSS_S_FAILURE
339 #endif
340
341 #define GSSEAP_KRB_INIT(ctx) do {                   \
342         OM_uint32 tmpMajor;                         \
343                                                     \
344         tmpMajor  = gssEapKerberosInit(minor, ctx); \
345         if (GSS_ERROR(tmpMajor)) {                  \
346             return tmpMajor;                        \
347         }                                           \
348     } while (0)
349
350 OM_uint32
351 gssEapKerberosInit(OM_uint32 *minor, krb5_context *context);
352
353 OM_uint32
354 rfc3961ChecksumTypeForKey(OM_uint32 *minor,
355                           krb5_keyblock *key,
356                           krb5_cksumtype *cksumtype);
357
358 krb5_const_principal
359 krbAnonymousPrincipal(void);
360
361 krb5_error_code
362 krbCryptoLength(krb5_context krbContext,
363 #ifdef HAVE_HEIMDAL_VERSION
364                 krb5_crypto krbCrypto,
365 #else
366                 krb5_keyblock *key,
367 #endif
368                 int type,
369                 size_t *length);
370
371 krb5_error_code
372 krbPaddingLength(krb5_context krbContext,
373 #ifdef HAVE_HEIMDAL_VERSION
374                  krb5_crypto krbCrypto,
375 #else
376                  krb5_keyblock *key,
377 #endif
378                  size_t dataLength,
379                  size_t *padLength);
380
381 krb5_error_code
382 krbBlockSize(krb5_context krbContext,
383 #ifdef HAVE_HEIMDAL_VERSION
384                  krb5_crypto krbCrypto,
385 #else
386                  krb5_keyblock *key,
387 #endif
388                  size_t *blockSize);
389
390 krb5_error_code
391 krbEnctypeToString(krb5_context krbContext,
392                    krb5_enctype enctype,
393                    const char *prefix,
394                    gss_buffer_t string);
395
396 krb5_error_code
397 krbMakeAuthDataKdcIssued(krb5_context context,
398                          const krb5_keyblock *key,
399                          krb5_const_principal issuer,
400 #ifdef HAVE_HEIMDAL_VERSION
401                          const AuthorizationData *authdata,
402                          AuthorizationData *adKdcIssued
403 #else
404                          krb5_authdata *const *authdata,
405                          krb5_authdata ***adKdcIssued
406 #endif
407                          );
408
409 krb5_error_code
410 krbMakeCred(krb5_context context,
411             krb5_auth_context authcontext,
412             krb5_creds *creds,
413             krb5_data *data);
414
415 /* util_lucid.c */
416 OM_uint32
417 gssEapExportLucidSecContext(OM_uint32 *minor,
418                             gss_ctx_id_t ctx,
419                             const gss_OID desiredObject,
420                             gss_buffer_set_t *data_set);
421
422 /* util_mech.c */
423 extern gss_OID GSS_EAP_MECHANISM;
424
425 int
426 gssEapInternalizeOid(const gss_OID oid,
427                      gss_OID *const pInternalizedOid);
428
429 OM_uint32
430 gssEapDefaultMech(OM_uint32 *minor,
431                   gss_OID *oid);
432
433 OM_uint32
434 gssEapIndicateMechs(OM_uint32 *minor,
435                     gss_OID_set *mechs);
436
437 OM_uint32
438 gssEapEnctypeToOid(OM_uint32 *minor,
439                    krb5_enctype enctype,
440                    gss_OID *pOid);
441
442 OM_uint32
443 gssEapOidToEnctype(OM_uint32 *minor,
444                    const gss_OID oid,
445                    krb5_enctype *enctype);
446
447 int
448 gssEapIsMechanismOid(const gss_OID oid);
449
450 int
451 gssEapIsConcreteMechanismOid(const gss_OID oid);
452
453 OM_uint32
454 gssEapValidateMechs(OM_uint32 *minor,
455                    const gss_OID_set mechs);
456
457 gss_buffer_t
458 gssEapOidToSaslName(const gss_OID oid);
459
460 gss_OID
461 gssEapSaslNameToOid(const gss_buffer_t name);
462
463 /* util_name.c */
464 #define EXPORT_NAME_FLAG_OID        0x1
465 #define EXPORT_NAME_FLAG_COMPOSITE  0x2
466
467 OM_uint32 gssEapAllocName(OM_uint32 *minor, gss_name_t *pName);
468 OM_uint32 gssEapReleaseName(OM_uint32 *minor, gss_name_t *pName);
469 OM_uint32 gssEapExportName(OM_uint32 *minor,
470                            const gss_name_t name,
471                            gss_buffer_t exportedName);
472 OM_uint32 gssEapExportNameInternal(OM_uint32 *minor,
473                                    const gss_name_t name,
474                                    gss_buffer_t exportedName,
475                                    unsigned int flags);
476 OM_uint32 gssEapImportName(OM_uint32 *minor,
477                            const gss_buffer_t input_name_buffer,
478                            gss_OID input_name_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 /* util_oid.c */
490 OM_uint32
491 composeOid(OM_uint32 *minor_status,
492            const char *prefix,
493            size_t prefix_len,
494            int suffix,
495            gss_OID_desc *oid);
496
497 OM_uint32
498 decomposeOid(OM_uint32 *minor_status,
499              const char *prefix,
500              size_t prefix_len,
501              gss_OID_desc *oid,
502              int *suffix) ;
503
504 OM_uint32
505 duplicateOid(OM_uint32 *minor_status,
506              const gss_OID_desc * const oid,
507              gss_OID *new_oid);
508
509 OM_uint32
510 duplicateOidSet(OM_uint32 *minor,
511                 const gss_OID_set src,
512                 gss_OID_set *dst);
513
514 static inline int
515 oidEqual(const gss_OID_desc *o1, const gss_OID_desc *o2)
516 {
517     if (o1 == GSS_C_NO_OID)
518         return (o2 == GSS_C_NO_OID);
519     else if (o2 == GSS_C_NO_OID)
520         return (o1 == GSS_C_NO_OID);
521     else
522         return (o1->length == o2->length &&
523                 memcmp(o1->elements, o2->elements, o1->length) == 0);
524 }
525
526 /* util_ordering.c */
527 OM_uint32
528 sequenceInternalize(OM_uint32 *minor,
529                     void **vqueue,
530                     unsigned char **buf,
531                     size_t *lenremain);
532
533 OM_uint32
534 sequenceExternalize(OM_uint32 *minor,
535                     void *vqueue,
536                     unsigned char **buf,
537                     size_t *lenremain);
538
539 size_t
540 sequenceSize(void *vqueue);
541
542 OM_uint32
543 sequenceFree(OM_uint32 *minor, void **vqueue);
544
545 OM_uint32
546 sequenceCheck(OM_uint32 *minor, void **vqueue, uint64_t seqnum);
547
548 OM_uint32
549 sequenceInit(OM_uint32 *minor, void **vqueue, uint64_t seqnum,
550              int do_replay, int do_sequence, int wide_nums);
551
552 /* util_token.c */
553 size_t
554 tokenSize(const gss_OID_desc *mech, size_t body_size);
555
556 void
557 makeTokenHeader(const gss_OID_desc *mech,
558                 size_t body_size,
559                 unsigned char **buf,
560                 enum gss_eap_token_type tok_type);
561
562 OM_uint32
563 verifyTokenHeader(OM_uint32 *minor,
564                   gss_OID mech,
565                   size_t *body_size,
566                   unsigned char **buf_in,
567                   size_t toksize_in,
568                   enum gss_eap_token_type *ret_tok_type);
569
570 /* Helper macros */
571
572 #define GSSEAP_CALLOC                   calloc
573 #define GSSEAP_MALLOC                   malloc
574 #define GSSEAP_FREE                     free
575 #define GSSEAP_REALLOC                  realloc
576
577 #define GSSEAP_NOT_IMPLEMENTED          do {            \
578         assert(0 && "not implemented");                 \
579         *minor = ENOSYS;                                \
580         return GSS_S_FAILURE;                           \
581     } while (0)
582
583 #include <pthread.h>
584
585 #define GSSEAP_MUTEX                    pthread_mutex_t
586 #define GSSEAP_MUTEX_INITIALIZER        PTHREAD_MUTEX_INITIALIZER
587
588 #define GSSEAP_MUTEX_INIT(m)            pthread_mutex_init((m), NULL)
589 #define GSSEAP_MUTEX_DESTROY(m)         pthread_mutex_destroy((m))
590 #define GSSEAP_MUTEX_LOCK(m)            pthread_mutex_lock((m))
591 #define GSSEAP_MUTEX_UNLOCK(m)          pthread_mutex_unlock((m))
592
593 #define GSSEAP_THREAD_KEY               pthread_key_t
594 #define GSSEAP_KEY_CREATE(k, d)         pthread_key_create((k), (d))
595 #define GSSEAP_GETSPECIFIC(k)           pthread_getspecific((k))
596 #define GSSEAP_SETSPECIFIC(k, d)        pthread_setspecific((k), (d))
597
598 #define GSSEAP_THREAD_ONCE              pthread_once_t
599 #define GSSEAP_ONCE(o, i)               pthread_once((o), (i))
600 #define GSSEAP_ONCE_INITIALIZER         PTHREAD_ONCE_INIT
601
602 /* Helper functions */
603 static inline void
604 store_uint16_be(uint16_t val, void *vp)
605 {
606     unsigned char *p = (unsigned char *)vp;
607
608     p[0] = (val >>  8) & 0xff;
609     p[1] = (val      ) & 0xff;
610 }
611
612 static inline uint16_t
613 load_uint16_be(const void *cvp)
614 {
615     const unsigned char *p = (const unsigned char *)cvp;
616
617     return (p[1] | (p[0] << 8));
618 }
619
620 static inline void
621 store_uint32_be(uint32_t val, void *vp)
622 {
623     unsigned char *p = (unsigned char *)vp;
624
625     p[0] = (val >> 24) & 0xff;
626     p[1] = (val >> 16) & 0xff;
627     p[2] = (val >>  8) & 0xff;
628     p[3] = (val      ) & 0xff;
629 }
630
631 static inline uint32_t
632 load_uint32_be(const void *cvp)
633 {
634     const unsigned char *p = (const unsigned char *)cvp;
635
636     return (p[3] | (p[2] << 8)
637             | ((uint32_t) p[1] << 16)
638             | ((uint32_t) p[0] << 24));
639 }
640
641 static inline void
642 store_uint64_be(uint64_t val, void *vp)
643 {
644     unsigned char *p = (unsigned char *)vp;
645
646     p[0] = (unsigned char)((val >> 56) & 0xff);
647     p[1] = (unsigned char)((val >> 48) & 0xff);
648     p[2] = (unsigned char)((val >> 40) & 0xff);
649     p[3] = (unsigned char)((val >> 32) & 0xff);
650     p[4] = (unsigned char)((val >> 24) & 0xff);
651     p[5] = (unsigned char)((val >> 16) & 0xff);
652     p[6] = (unsigned char)((val >>  8) & 0xff);
653     p[7] = (unsigned char)((val      ) & 0xff);
654 }
655
656 static inline uint64_t
657 load_uint64_be(const void *cvp)
658 {
659     const unsigned char *p = (const unsigned char *)cvp;
660
661     return ((uint64_t)load_uint32_be(p) << 32) | load_uint32_be(p + 4);
662 }
663
664 static inline unsigned char *
665 store_buffer(gss_buffer_t buffer, void *vp, int wide_nums)
666 {
667     unsigned char *p = (unsigned char *)vp;
668
669     if (wide_nums) {
670         store_uint64_be(buffer->length, p);
671         p += 8;
672     } else {
673         store_uint32_be(buffer->length, p);
674         p += 4;
675     }
676
677     if (buffer->value != NULL) {
678         memcpy(p, buffer->value, buffer->length);
679         p += buffer->length;
680     }
681
682     return p;
683 }
684
685 static inline unsigned char *
686 load_buffer(const void *cvp, size_t length, gss_buffer_t buffer)
687 {
688     buffer->length = 0;
689     buffer->value = GSSEAP_MALLOC(length);
690     if (buffer->value == NULL)
691         return NULL;
692     buffer->length = length;
693     memcpy(buffer->value, cvp, length);
694     return (unsigned char *)cvp + length;
695 }
696
697 static inline unsigned char *
698 store_oid(gss_OID oid, void *vp)
699 {
700     gss_buffer_desc buf;
701
702     if (oid != GSS_C_NO_OID) {
703         buf.length = oid->length;
704         buf.value = oid->elements;
705     } else {
706         buf.length = 0;
707         buf.value = NULL;
708     }
709
710     return store_buffer(&buf, vp, FALSE);
711 }
712
713 static inline void
714 krbDataToGssBuffer(krb5_data *data, gss_buffer_t buffer)
715 {
716     buffer->value = (void *)data->data;
717     buffer->length = data->length;
718 }
719
720 static inline void
721 krbPrincComponentToGssBuffer(krb5_principal krbPrinc,
722                              int index, gss_buffer_t buffer)
723 {
724 #ifdef HAVE_HEIMDAL_VERSION
725     buffer->value = (void *)KRB_PRINC_NAME(krbPrinc)[index];
726     buffer->length = strlen((char *)buffer->value);
727 #else
728     buffer->value = (void *)krb5_princ_component(NULL, krbPrinc, index)->data;
729     buffer->length = krb5_princ_component(NULL, krbPrinc, index)->length;
730 #endif /* HAVE_HEIMDAL_VERSION */
731 }
732
733 static inline void
734 krbPrincRealmToGssBuffer(krb5_principal krbPrinc, gss_buffer_t buffer)
735 {
736 #ifdef HAVE_HEIMDAL_VERSION
737     buffer->value = (void *)KRB_PRINC_REALM(krbPrinc);
738     buffer->length = strlen((char *)buffer->value);
739 #else
740     krbDataToGssBuffer(KRB_PRINC_REALM(krbPrinc), buffer);
741 #endif
742 }
743
744 static inline void
745 gssBufferToKrbData(gss_buffer_t buffer, krb5_data *data)
746 {
747     data->data = (char *)buffer->value;
748     data->length = buffer->length;
749 }
750
751 #ifdef __cplusplus
752 }
753 #endif
754
755 #include "util_attr.h"
756 #ifdef GSSEAP_ENABLE_REAUTH
757 #include "util_reauth.h"
758 #endif
759
760 #endif /* _UTIL_H_ */