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