initial TLV refactor
[mech_eap.orig] / 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_ESTABLISH_CONTEXT       = 0x0601,  /* establish context */
158 };
159
160 /* inner token types and flags */
161 #define ITOK_TYPE_NONE                  0x00000000
162 #define ITOK_TYPE_CONTEXT_ERR           0x00000001
163 #define ITOK_TYPE_ACCEPTOR_NAME_REQ     0x00000002
164 #define ITOK_TYPE_ACCEPTOR_NAME_RESP    0x00000003
165 #define ITOK_TYPE_EAP_RESP              0x00000004
166 #define ITOK_TYPE_EAP_REQ               0x00000005
167 #define ITOK_TYPE_GSS_CHANNEL_BINDINGS  0x00000006
168 #define ITOK_TYPE_REAUTH_CREDS          0x00000007
169 #define ITOK_TYPE_REAUTH_REQ            0x00000008
170 #define ITOK_TYPE_REAUTH_RESP           0x00000009
171
172 #define ITOK_FLAG_CRITICAL              0x80000000  /* critical, wire flag */
173 #define ITOK_FLAG_VERIFIED              0x40000000  /* verified, API flag */
174
175 #define ITOK_TYPE_MASK                  (~(EXT_FLAG_CRITICAL | EXT_FLAG_VERIFIED))
176
177 OM_uint32 gssEapAllocContext(OM_uint32 *minor, gss_ctx_id_t *pCtx);
178 OM_uint32 gssEapReleaseContext(OM_uint32 *minor, gss_ctx_id_t *pCtx);
179
180 OM_uint32
181 gssEapMakeToken(OM_uint32 *minor,
182                 gss_ctx_id_t ctx,
183                 const gss_buffer_t innerToken,
184                 enum gss_eap_token_type tokenType,
185                 gss_buffer_t outputToken);
186
187 OM_uint32
188 gssEapVerifyToken(OM_uint32 *minor,
189                   gss_ctx_id_t ctx,
190                   const gss_buffer_t inputToken,
191                   enum gss_eap_token_type *tokenType,
192                   gss_buffer_t innerInputToken);
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 gssEapDisplayName(OM_uint32 *minor,
201                   gss_name_t name,
202                   gss_buffer_t output_name_buffer,
203                   gss_OID *output_name_type);
204
205 /* util_cred.c */
206 OM_uint32 gssEapAllocCred(OM_uint32 *minor, gss_cred_id_t *pCred);
207 OM_uint32 gssEapReleaseCred(OM_uint32 *minor, gss_cred_id_t *pCred);
208
209 OM_uint32
210 gssEapAcquireCred(OM_uint32 *minor,
211                   const gss_name_t desiredName,
212                   const gss_buffer_t password,
213                   OM_uint32 timeReq,
214                   const gss_OID_set desiredMechs,
215                   int cred_usage,
216                   gss_cred_id_t *pCred,
217                   gss_OID_set *pActualMechs,
218                   OM_uint32 *timeRec);
219
220 int gssEapCredAvailable(gss_cred_id_t cred, gss_OID mech);
221
222 /* util_crypt.c */
223 int
224 gssEapEncrypt(krb5_context context, int dce_style, size_t ec,
225               size_t rrc,
226 #ifdef HAVE_HEIMDAL_VERSION
227               krb5_crypto crypto,
228 #else
229               krb5_keyblock *key,
230 #endif
231               int usage,
232               gss_iov_buffer_desc *iov, int iov_count);
233
234 int
235 gssEapDecrypt(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 gssEapMapCryptoFlag(OM_uint32 type);
247
248 gss_iov_buffer_t
249 gssEapLocateIov(gss_iov_buffer_desc *iov,
250                 int iov_count,
251                 OM_uint32 type);
252
253 void
254 gssEapIovMessageLength(gss_iov_buffer_desc *iov,
255                        int iov_count,
256                        size_t *data_length,
257                        size_t *assoc_data_length);
258
259 void
260 gssEapReleaseIov(gss_iov_buffer_desc *iov, int iov_count);
261
262 int
263 gssEapIsIntegrityOnly(gss_iov_buffer_desc *iov, int iov_count);
264
265 int
266 gssEapAllocIov(gss_iov_buffer_t iov, size_t size);
267
268 OM_uint32
269 gssEapDeriveRfc3961Key(OM_uint32 *minor,
270                        const unsigned char *key,
271                        size_t keyLength,
272                        krb5_enctype enctype,
273                        krb5_keyblock *pKey);
274
275 /* util_krb.c */
276 #ifdef HAVE_HEIMDAL_VERSION
277
278 #define KRB_TIME_FOREVER        ((time_t)~0L)
279
280 #define KRB_KEY_TYPE(key)       ((key)->keytype)
281 #define KRB_KEY_DATA(key)       ((key)->keyvalue.data)
282 #define KRB_KEY_LENGTH(key)     ((key)->keyvalue.length)
283
284 #define KRB_PRINC_LENGTH(princ) ((princ)->name.name_string.len)
285 #define KRB_PRINC_TYPE(princ)   ((princ)->name.name_type)
286 #define KRB_PRINC_NAME(princ)   ((princ)->name.name_string.val)
287 #define KRB_PRINC_REALM(princ)  ((princ)->realm)
288
289 #define KRB_KT_ENT_KEYBLOCK(e)  (&(e)->keyblock)
290 #define KRB_KT_ENT_FREE(c, e)   krb5_kt_free_entry((c), (e))
291
292 #define KRB_CRYPTO_CONTEXT(ctx) (krbCrypto)
293
294 #else
295
296 #define KRB_TIME_FOREVER        KRB5_INT32_MAX
297
298 #define KRB_KEY_TYPE(key)       ((key)->enctype)
299 #define KRB_KEY_DATA(key)       ((key)->contents)
300 #define KRB_KEY_LENGTH(key)     ((key)->length)
301
302 #define KRB_PRINC_LENGTH(princ) (krb5_princ_size(NULL, (princ)))
303 #define KRB_PRINC_TYPE(princ)   (krb5_princ_type(NULL, (princ)))
304 #define KRB_PRINC_NAME(princ)   (krb5_princ_name(NULL, (princ)))
305 #define KRB_PRINC_REALM(princ)  (krb5_princ_realm(NULL, (princ)))
306
307 #define KRB_KT_ENT_KEYBLOCK(e)  (&(e)->key)
308 #define KRB_KT_ENT_FREE(c, e)   krb5_free_keytab_entry_contents((c), (e))
309
310 #define KRB_CRYPTO_CONTEXT(ctx) (&(ctx)->rfc3961Key)
311
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 #define GSSEAP_KRB_INIT(ctx) do {                   \
321         OM_uint32 tmpMajor;                         \
322                                                     \
323         tmpMajor  = gssEapKerberosInit(minor, ctx); \
324         if (GSS_ERROR(tmpMajor)) {                  \
325             return tmpMajor;                        \
326         }                                           \
327     } while (0)
328
329 OM_uint32
330 gssEapKerberosInit(OM_uint32 *minor, krb5_context *context);
331
332 OM_uint32
333 rfc3961ChecksumTypeForKey(OM_uint32 *minor,
334                           krb5_keyblock *key,
335                           krb5_cksumtype *cksumtype);
336
337 krb5_const_principal
338 krbAnonymousPrincipal(void);
339
340 krb5_error_code
341 krbCryptoLength(krb5_context krbContext,
342 #ifdef HAVE_HEIMDAL_VERSION
343                 krb5_crypto krbCrypto,
344 #else
345                 krb5_keyblock *key,
346 #endif
347                 int type,
348                 size_t *length);
349
350 krb5_error_code
351 krbPaddingLength(krb5_context krbContext,
352 #ifdef HAVE_HEIMDAL_VERSION
353                  krb5_crypto krbCrypto,
354 #else
355                  krb5_keyblock *key,
356 #endif
357                  size_t dataLength,
358                  size_t *padLength);
359
360 krb5_error_code
361 krbBlockSize(krb5_context krbContext,
362 #ifdef HAVE_HEIMDAL_VERSION
363                  krb5_crypto krbCrypto,
364 #else
365                  krb5_keyblock *key,
366 #endif
367                  size_t *blockSize);
368
369 krb5_error_code
370 krbEnctypeToString(krb5_context krbContext,
371                    krb5_enctype enctype,
372                    const char *prefix,
373                    gss_buffer_t string);
374
375 krb5_error_code
376 krbMakeAuthDataKdcIssued(krb5_context context,
377                          const krb5_keyblock *key,
378                          krb5_const_principal issuer,
379 #ifdef HAVE_HEIMDAL_VERSION
380                          const AuthorizationData *authdata,
381                          AuthorizationData *adKdcIssued
382 #else
383                          krb5_authdata *const *authdata,
384                          krb5_authdata ***adKdcIssued
385 #endif
386                          );
387
388 krb5_error_code
389 krbMakeCred(krb5_context context,
390             krb5_auth_context authcontext,
391             krb5_creds *creds,
392             krb5_data *data);
393
394 /* util_lucid.c */
395 OM_uint32
396 gssEapExportLucidSecContext(OM_uint32 *minor,
397                             gss_ctx_id_t ctx,
398                             const gss_OID desiredObject,
399                             gss_buffer_set_t *data_set);
400
401 /* util_mech.c */
402 extern gss_OID GSS_EAP_MECHANISM;
403
404 int
405 gssEapInternalizeOid(const gss_OID oid,
406                      gss_OID *const pInternalizedOid);
407
408 OM_uint32
409 gssEapReleaseOid(OM_uint32 *minor, gss_OID *oid);
410
411 OM_uint32
412 gssEapDefaultMech(OM_uint32 *minor,
413                   gss_OID *oid);
414
415 OM_uint32
416 gssEapIndicateMechs(OM_uint32 *minor,
417                     gss_OID_set *mechs);
418
419 OM_uint32
420 gssEapEnctypeToOid(OM_uint32 *minor,
421                    krb5_enctype enctype,
422                    gss_OID *pOid);
423
424 OM_uint32
425 gssEapOidToEnctype(OM_uint32 *minor,
426                    const gss_OID oid,
427                    krb5_enctype *enctype);
428
429 int
430 gssEapIsMechanismOid(const gss_OID oid);
431
432 int
433 gssEapIsConcreteMechanismOid(const gss_OID oid);
434
435 OM_uint32
436 gssEapValidateMechs(OM_uint32 *minor,
437                    const gss_OID_set mechs);
438
439 gss_buffer_t
440 gssEapOidToSaslName(const gss_OID oid);
441
442 gss_OID
443 gssEapSaslNameToOid(const gss_buffer_t name);
444
445 /* util_name.c */
446 #define EXPORT_NAME_FLAG_OID        0x1
447 #define EXPORT_NAME_FLAG_COMPOSITE  0x2
448
449 OM_uint32 gssEapAllocName(OM_uint32 *minor, gss_name_t *pName);
450 OM_uint32 gssEapReleaseName(OM_uint32 *minor, gss_name_t *pName);
451 OM_uint32 gssEapExportName(OM_uint32 *minor,
452                            const gss_name_t name,
453                            gss_buffer_t exportedName);
454 OM_uint32 gssEapExportNameInternal(OM_uint32 *minor,
455                                    const gss_name_t name,
456                                    gss_buffer_t exportedName,
457                                    unsigned int flags);
458 OM_uint32 gssEapImportName(OM_uint32 *minor,
459                            const gss_buffer_t input_name_buffer,
460                            gss_OID input_name_type,
461                            gss_name_t *output_name);
462 OM_uint32 gssEapImportNameInternal(OM_uint32 *minor,
463                                    const gss_buffer_t input_name_buffer,
464                                    gss_name_t *output_name,
465                                    unsigned int flags);
466 OM_uint32
467 gssEapDuplicateName(OM_uint32 *minor,
468                     const gss_name_t input_name,
469                     gss_name_t *dest_name);
470
471 /* util_oid.c */
472 OM_uint32
473 composeOid(OM_uint32 *minor_status,
474            const char *prefix,
475            size_t prefix_len,
476            int suffix,
477            gss_OID_desc *oid);
478
479 OM_uint32
480 decomposeOid(OM_uint32 *minor_status,
481              const char *prefix,
482              size_t prefix_len,
483              gss_OID_desc *oid,
484              int *suffix) ;
485
486 OM_uint32
487 duplicateOid(OM_uint32 *minor_status,
488              const gss_OID_desc * const oid,
489              gss_OID *new_oid);
490
491 OM_uint32
492 duplicateOidSet(OM_uint32 *minor,
493                 const gss_OID_set src,
494                 gss_OID_set *dst);
495
496 static inline int
497 oidEqual(const gss_OID_desc *o1, const gss_OID_desc *o2)
498 {
499     if (o1 == GSS_C_NO_OID)
500         return (o2 == GSS_C_NO_OID);
501     else if (o2 == GSS_C_NO_OID)
502         return (o1 == GSS_C_NO_OID);
503     else
504         return (o1->length == o2->length &&
505                 memcmp(o1->elements, o2->elements, o1->length) == 0);
506 }
507
508 /* util_ordering.c */
509 OM_uint32
510 sequenceInternalize(OM_uint32 *minor,
511                     void **vqueue,
512                     unsigned char **buf,
513                     size_t *lenremain);
514
515 OM_uint32
516 sequenceExternalize(OM_uint32 *minor,
517                     void *vqueue,
518                     unsigned char **buf,
519                     size_t *lenremain);
520
521 size_t
522 sequenceSize(void *vqueue);
523
524 OM_uint32
525 sequenceFree(OM_uint32 *minor, void **vqueue);
526
527 OM_uint32
528 sequenceCheck(OM_uint32 *minor, void **vqueue, uint64_t seqnum);
529
530 OM_uint32
531 sequenceInit(OM_uint32 *minor, void **vqueue, uint64_t seqnum,
532              int do_replay, int do_sequence, int wide_nums);
533
534 /* util_sm.c */
535 struct gss_eap_sm;
536
537 OM_uint32
538 gssEapSmStep(OM_uint32 *minor,
539              gss_cred_id_t cred,
540              gss_ctx_id_t ctx,
541              gss_name_t target,
542              gss_OID mech,
543              OM_uint32 reqFlags,
544              OM_uint32 timeReq,
545              gss_channel_bindings_t chanBindings,
546              gss_buffer_t inputToken,
547              gss_buffer_t outputToken,
548              struct gss_eap_sm *sm,
549              size_t smCount);
550
551 /* util_token.c */
552 OM_uint32
553 gssEapEncodeInnerTokens(OM_uint32 *minor,
554                         gss_buffer_set_t extensions,
555                         OM_uint32 *types,
556                         gss_buffer_t buffer);
557 OM_uint32
558 gssEapDecodeInnerTokens(OM_uint32 *minor,
559                         const gss_buffer_t buffer,
560                         gss_buffer_set_t *pExtensions,
561                         OM_uint32 **pTypes);
562
563 size_t
564 tokenSize(const gss_OID_desc *mech, size_t body_size);
565
566 void
567 makeTokenHeader(const gss_OID_desc *mech,
568                 size_t body_size,
569                 unsigned char **buf,
570                 enum gss_eap_token_type tok_type);
571
572 OM_uint32
573 verifyTokenHeader(OM_uint32 *minor,
574                   gss_OID mech,
575                   size_t *body_size,
576                   unsigned char **buf_in,
577                   size_t toksize_in,
578                   enum gss_eap_token_type *ret_tok_type);
579
580 /* Helper macros */
581
582 #define GSSEAP_CALLOC                   calloc
583 #define GSSEAP_MALLOC                   malloc
584 #define GSSEAP_FREE                     free
585 #define GSSEAP_REALLOC                  realloc
586
587 #define GSSEAP_NOT_IMPLEMENTED          do {            \
588         assert(0 && "not implemented");                 \
589         *minor = ENOSYS;                                \
590         return GSS_S_FAILURE;                           \
591     } while (0)
592
593 #include <pthread.h>
594
595 #define GSSEAP_MUTEX                    pthread_mutex_t
596 #define GSSEAP_MUTEX_INITIALIZER        PTHREAD_MUTEX_INITIALIZER
597
598 #define GSSEAP_MUTEX_INIT(m)            pthread_mutex_init((m), NULL)
599 #define GSSEAP_MUTEX_DESTROY(m)         pthread_mutex_destroy((m))
600 #define GSSEAP_MUTEX_LOCK(m)            pthread_mutex_lock((m))
601 #define GSSEAP_MUTEX_UNLOCK(m)          pthread_mutex_unlock((m))
602
603 #define GSSEAP_THREAD_KEY               pthread_key_t
604 #define GSSEAP_KEY_CREATE(k, d)         pthread_key_create((k), (d))
605 #define GSSEAP_GETSPECIFIC(k)           pthread_getspecific((k))
606 #define GSSEAP_SETSPECIFIC(k, d)        pthread_setspecific((k), (d))
607
608 #define GSSEAP_THREAD_ONCE              pthread_once_t
609 #define GSSEAP_ONCE(o, i)               pthread_once((o), (i))
610 #define GSSEAP_ONCE_INITIALIZER         PTHREAD_ONCE_INIT
611
612 /* Helper functions */
613 static inline void
614 store_uint16_be(uint16_t val, void *vp)
615 {
616     unsigned char *p = (unsigned char *)vp;
617
618     p[0] = (val >>  8) & 0xff;
619     p[1] = (val      ) & 0xff;
620 }
621
622 static inline uint16_t
623 load_uint16_be(const void *cvp)
624 {
625     const unsigned char *p = (const unsigned char *)cvp;
626
627     return (p[1] | (p[0] << 8));
628 }
629
630 static inline void
631 store_uint32_be(uint32_t val, void *vp)
632 {
633     unsigned char *p = (unsigned char *)vp;
634
635     p[0] = (val >> 24) & 0xff;
636     p[1] = (val >> 16) & 0xff;
637     p[2] = (val >>  8) & 0xff;
638     p[3] = (val      ) & 0xff;
639 }
640
641 static inline uint32_t
642 load_uint32_be(const void *cvp)
643 {
644     const unsigned char *p = (const unsigned char *)cvp;
645
646     return (p[3] | (p[2] << 8)
647             | ((uint32_t) p[1] << 16)
648             | ((uint32_t) p[0] << 24));
649 }
650
651 static inline void
652 store_uint64_be(uint64_t val, void *vp)
653 {
654     unsigned char *p = (unsigned char *)vp;
655
656     p[0] = (unsigned char)((val >> 56) & 0xff);
657     p[1] = (unsigned char)((val >> 48) & 0xff);
658     p[2] = (unsigned char)((val >> 40) & 0xff);
659     p[3] = (unsigned char)((val >> 32) & 0xff);
660     p[4] = (unsigned char)((val >> 24) & 0xff);
661     p[5] = (unsigned char)((val >> 16) & 0xff);
662     p[6] = (unsigned char)((val >>  8) & 0xff);
663     p[7] = (unsigned char)((val      ) & 0xff);
664 }
665
666 static inline uint64_t
667 load_uint64_be(const void *cvp)
668 {
669     const unsigned char *p = (const unsigned char *)cvp;
670
671     return ((uint64_t)load_uint32_be(p) << 32) | load_uint32_be(p + 4);
672 }
673
674 static inline unsigned char *
675 store_buffer(gss_buffer_t buffer, void *vp, int wide_nums)
676 {
677     unsigned char *p = (unsigned char *)vp;
678
679     if (wide_nums) {
680         store_uint64_be(buffer->length, p);
681         p += 8;
682     } else {
683         store_uint32_be(buffer->length, p);
684         p += 4;
685     }
686
687     if (buffer->value != NULL) {
688         memcpy(p, buffer->value, buffer->length);
689         p += buffer->length;
690     }
691
692     return p;
693 }
694
695 static inline unsigned char *
696 load_buffer(const void *cvp, size_t length, gss_buffer_t buffer)
697 {
698     buffer->length = 0;
699     buffer->value = GSSEAP_MALLOC(length);
700     if (buffer->value == NULL)
701         return NULL;
702     buffer->length = length;
703     memcpy(buffer->value, cvp, length);
704     return (unsigned char *)cvp + length;
705 }
706
707 static inline unsigned char *
708 store_oid(gss_OID oid, void *vp)
709 {
710     gss_buffer_desc buf;
711
712     if (oid != GSS_C_NO_OID) {
713         buf.length = oid->length;
714         buf.value = oid->elements;
715     } else {
716         buf.length = 0;
717         buf.value = NULL;
718     }
719
720     return store_buffer(&buf, vp, FALSE);
721 }
722
723 static inline void
724 krbDataToGssBuffer(krb5_data *data, gss_buffer_t buffer)
725 {
726     buffer->value = (void *)data->data;
727     buffer->length = data->length;
728 }
729
730 static inline void
731 krbPrincComponentToGssBuffer(krb5_principal krbPrinc,
732                              int index, gss_buffer_t buffer)
733 {
734 #ifdef HAVE_HEIMDAL_VERSION
735     buffer->value = (void *)KRB_PRINC_NAME(krbPrinc)[index];
736     buffer->length = strlen((char *)buffer->value);
737 #else
738     buffer->value = (void *)krb5_princ_component(NULL, krbPrinc, index)->data;
739     buffer->length = krb5_princ_component(NULL, krbPrinc, index)->length;
740 #endif /* HAVE_HEIMDAL_VERSION */
741 }
742
743 static inline void
744 krbPrincRealmToGssBuffer(krb5_principal krbPrinc, gss_buffer_t buffer)
745 {
746 #ifdef HAVE_HEIMDAL_VERSION
747     buffer->value = (void *)KRB_PRINC_REALM(krbPrinc);
748     buffer->length = strlen((char *)buffer->value);
749 #else
750     krbDataToGssBuffer(KRB_PRINC_REALM(krbPrinc), buffer);
751 #endif
752 }
753
754 static inline void
755 gssBufferToKrbData(gss_buffer_t buffer, krb5_data *data)
756 {
757     data->data = (char *)buffer->value;
758     data->length = buffer->length;
759 }
760
761 #ifdef __cplusplus
762 }
763 #endif
764
765 #include "util_attr.h"
766 #ifdef GSSEAP_ENABLE_REAUTH
767 #include "util_reauth.h"
768 #endif
769
770 #endif /* _UTIL_H_ */