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