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